Need help with rotation to direction of movement

Hello I m trying to make game with geometry wars / superstardust way of controlls .

right now i am using his code

Hello  I m trying to make game with geometry wars / superstardust way of controlls .

right now i am using his code
using UnityEngine;
using System.Collections;

public class GWConntorls : MonoBehaviour {
 

    public Vector3 startPosition;
    public Vector2 speed = new Vector2(1,0);
 
    void Start(){
     

 
    }
    void Update () {
     
     
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis ("Vertical");
     

     
        Vector3 movement = new Vector3
            (speed.x*inputX, speed.y*inputY,0);
     
        movement*= Time.deltaTime;
        transform.Translate(movement)

    }   
    }

But what I need is for the object seen from the top to rotate in direction of movement .
Also I think that it moves too fast in diagonal

And Im not sure how to do that , I’m no programer :frowning:

Could anyone help me ?
thank you

If I understand it right, because you’re using TRANSLATE, you’re adding a specific amount of X and Y movement based on user input. This means that when you move in both X AND Y at the same time, ie diagonal, you will add all of the x distance and all of the y distance, which, on a diagonal, is further than a horizontal or vertical movement alone. ie you’re moving across the diagonal of a box, instead of along the edges.

What you need is that, when the user is moving in X and Y at the same time, scale down the distance moved.

Since you’re apparently not simulating 360 degree movement (because you’d have to maintain a vector or angle of movement), you actually are doing 8-way movement right now. So all you’d need to do is detect when both inputX and inputY are not zero … and in that case, divide both speed.x and speed.y by around 1.414213562, which roughly gives you the hypotenuse of the diagonal without dipping into cos/sine math. See how that works out.

Otherwise you’re going to need to keep track of an angle, and a speed, as single numbers, and then when there is user input, convert the input to a vector or to an angle modifcation, and then use like transform.localPosition.x += speed * Mathf.cos(angle); and similarly transform.localPosition.y += speed * Mathf.sin(angle); or something like that… to get an x and y amount to add to the position.

1 Like

so like

if (inputX != 0 && inputY != 0){
speed.x = speed.x / 1.414213562;
speed.y = speed.y / 1.414213562;
}

before you calculate the movement variable. but if this will happen every frame, so every frame you’ll need to start out with initializing speed.x and y to their defaults.