#Need help with 2d rotation in my new game.

1 messages · Page 1 of 1 (latest)

ancient axle
#

transform.right = velocity alone should be enough to make it rotate towards the movement direction

worthy junco
#

Hi brother

#

I'm kinda of noob

wanton dust
#

bro is making geometry dash

worthy junco
worthy junco
worthy junco
white dagger
worthy junco
ancient axle
ancient axle
# worthy junco Check this out

Well, for me that seems to correlate with the velocity pretty accurately. Your movement is just very far a way from the one in the example, that movement looks a lot more like the zig zag one. If you manage to make the movement of the plane better, it will work nicely, i, pretty confident on that

worthy junco
#

Actually i want to do something like this

ancient axle
worthy junco
ancient axle
#

Still not even close

worthy junco
worthy junco
ancient axle
# worthy junco Can you give me some suggestions regarding that

Well, now that I look your code closer, it doesnt make much sense to me. Theres so many things that do nothing and those lines that actually do, seems like they are meant to do something much different than what they do now. Id just throw all that movement code into trash bin and start again. This is how I would approach this movement: it seems the x movement speed is always constant, no need to touch that. Y velocity on the other hand should increase when player presses a key and decrease when they dont. So you could make two variables: speedX which you only change in inspector and never via script and speedY which you change like this (pseudocode):

if key is pressed then
    increase speedY by rate of rotSpeed units per second
else
    decrease speedY by rate of rotSpeed units per second

After that you can just set rb.velocity = new Vector2(speedX, speedY); and it should work nicely. If it starts working, you can then consider clamping the speedY to some reasonable range but only after the basic movement works. Btw. loading .txt files to my laptop/phone whatever and opening them is annoying, use either codeblocks or paste sites for longer codes, its much easier for us and they both support syntax highlighting which makes the code much easier to read

worthy junco
#

Thanks brother for your help

#

Let me try with your method

#

Also after that how should I attempt the rotation?

ancient axle
worthy junco
#

Thanks brother

worthy junco
ancient axle
worthy junco
# ancient axle could you show the code again so I could check if the code is ok?

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class SpaceshipController : MonoBehaviour
{
[Header("Components")]
private Rigidbody2D rb;

[Header("Player Movement")]
[SerializeField] float xSpeed;
float ySpeed;
[SerializeField] float minOffset;
[SerializeField] float maxOffset;

[Header("Player Rotation")]
[SerializeField] float rotSpeed;
[SerializeField] float rotIntensity;


// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    if (Input.GetMouseButton(0))
    {
        ySpeed += rotSpeed * Time.deltaTime;
    }
    else
    {
        ySpeed -= rotSpeed * Time.deltaTime;
    }

    rb.velocity = new Vector2(xSpeed, ySpeed);

    transform.right = rb.velocity;
    

    transform.position = new Vector2(transform.position.x, Mathf.Clamp(transform.position.y, minOffset, maxOffset));
}

}

ancient axle
#

[]cb

final scrollBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

worthy junco
#
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class SpaceshipController : MonoBehaviour
{
    [Header("Components")]
    private Rigidbody2D rb;

    [Header("Player Movement")]
    [SerializeField] float xSpeed;
    float ySpeed;
    [SerializeField] float minOffset;
    [SerializeField] float maxOffset;

    [Header("Player Rotation")]
    [SerializeField] float rotSpeed;
    [SerializeField] float rotIntensity;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            ySpeed += rotSpeed * Time.deltaTime;
        }
        else
        {
            ySpeed -= rotSpeed * Time.deltaTime;
        }

        rb.velocity = new Vector2(xSpeed, ySpeed);

        transform.right = rb.velocity;
        

        transform.position = new Vector2(transform.position.x, Mathf.Clamp(transform.position.y, minOffset, maxOffset));
    }
}

worthy junco
#

Thanks Brother

ancient axle
#

I'd put that in FixedUpdate instead of Update tho

worthy junco
#

Yeah Sure I'll update that

#

Can you give me some tips regarding how should i learn more?

#

I did one course of unity on udemy now i'm proceeding to do my own small games

ancient axle
# worthy junco Can you give me some tips regarding how should i learn more?

just add small aspects to the game, watch tutorial video if you don't know how. score system? save highscore? spawn random obstacles (flappy bird like obstacles, you could make some premade set of obstacles that you can spawn at different height)? or maybe just handcraft multiple maps with increasing difficulty? make the game end when you collide with something? make the game get increasingly hard (everything gets faster over time? the longer you survive, the better)? add sound effects? maybe some visual effects?

worthy junco
#

Thanks brother

#

btw I'm a graphic designer too if you need any graphic designs then let me know