T
T
twilighthasan2020-04-22 17:28:22
C++ / C#
twilighthasan, 2020-04-22 17:28:22

An object that moves to the side, when moving diagonally, the speed increases unity C#?

float _x, _y;
    public float speed = 10.0f;

    void FixedUpdate()
    {


        _x = Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed;
        _y = Input.GetAxis("Vertical") * Time.fixedDeltaTime * speed;
        Vector3 v = new Vector3(_x, _y);
        print("Скорость движение " + rb.velocity.magnitude);

        rb.AddForce(v, ForceMode.Impulse);


        if (_x == 0 || _y == 0)
        {
            rb.velocity = Vector3.zero;
        }
        else
        {
            rb.velocity = Vector2.ClampMagnitude(v, 0);
        }



    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2020-04-22
@twilighthasan

I suggest you draw on a piece of paper in a box.
a force vector with a unit size in X (1; 0)
then a force vector with a unit size in Y (0; 1)
now a folded vector (1; 1)
you will be surprised to see that the "diagonal" (vector size) will be larger. respectively, and strength.
how to fix? usually if it's just a direction vector, then it will be normalized.
I hope it was clear. good luck)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question