Y
Y
yraiv2022-02-08 13:46:28
Unity
yraiv, 2022-02-08 13:46:28

How to set an impulse in the direction of motion in 2d?

2d game, you need to set an impulse so that the bullet moves in the direction the parent object is looking. Of all that I went through, only came up

void Fire()
    {
        Debug.Log("Fire");

        curTimeout += Time.deltaTime;
        if (curTimeout > fireRate)
        {
            curTimeout = 0;
            Rigidbody2D clone = Instantiate(bullet, gunPoint.position, Quaternion.identity) as Rigidbody2D;
            clone.transform.up = gunPoint.up;
            clone.velocity = clone.transform.up * speed;
            Debug.Log("выстрел произошёл");

        }
    }


And then, you have to change the rotation at the bullet spawn point to -180. Only then does it shoot straight. Although all other objects are set to 0 in rotation, where did I mess up?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question