FoxTreeMoon
FoxTreeMoon

Reputation: 13

Why is my c# code making going left and right much faster than forward and backwards

I'm trying to code some custom controls for some reason and for some reason going left and right is 100 times faster than going forwards and backwards. I've spent forever trying to fix it. I'm getting pretty mad and tired trying to fix it so please help me.

    void Start()
    {
        xVelocity = 0f;
        yVelocity = 0f;
        zVelocity = 0f;
    }

    void Update()
    {
        yVelocity--;
        Control();
        transform.position += transform.up * Time.deltaTime * yVelocity;
        transform.position += transform.forward * Time.deltaTime * zVelocity;
        transform.position += transform.TransformVector(-90, 0, 0) * Time.deltaTime * xVelocity;
    }

    private void Control()
    {
        if (Input.GetKey(KeyCode.W))
        {
            zVelocity += speed;
        }
        if (Input.GetKey(KeyCode.S))
        {
            zVelocity += -speed;
        }
        if (Input.GetKey(KeyCode.A))
        {
            xVelocity += speed;
        }
        if (Input.GetKey(KeyCode.D))
        {
            xVelocity += -speed;
        }
        if (Input.GetKey(KeyCode.W) == false && Input.GetKey(KeyCode.S) == false)
        {

            if (zVelocity == 0 == false)
            {
                if (zVelocity < 0)
                {
                    zVelocity += friction;
                }
                else
                {
                    zVelocity -= friction;
                }
            }
        }
        if (Input.GetKey(KeyCode.A) == false && Input.GetKey(KeyCode.D) == false)
        {
            if (xVelocity == 0 == false)
            {
                if (xVelocity < 0)
                {
                    xVelocity += friction;
                }
                else
                {
                    xVelocity -= friction;
                }
            }

        }

Upvotes: 1

Views: 49

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70671

I'm trying to code some custom controls for some reason and for some reason going left and right is 100 times faster than going forwards and backwards

Without a complete code example, it's impossible to know for sure what the problem is. But…

I'll bet that it's not 100X, but rather 90X.

The up and forward vectors are unit vectors, i.e. magnitude of 1. But you are transforming your position along the X direction with an explicitly-constructed vector that has a magnitude of 90 (transform.TransformVector(-90, 0, 0)). So your left/right movement is 90X the magnitude of forward/back and up/down.

You should able to change the code to transform.TransformVector(-1, 0, 0) (a unit vector) and get the behavior you want.

Upvotes: 2

Related Questions