- Home /
Question by
unity_418scarlet · Nov 11, 2021 at 05:12 PM ·
rotationshooting
bullet shot from Y axis while sprite 2D rotates on z axis
Hello I just finished looking for answers in the forum but I did not find an answer
I have a simple sprite 2d hexagonal, i make it rotating since start , this sprite shoots a projectile but the projectile always shoot in the same direction without following the rotation of the sprite. what am i missing? thank you
GameObject laser = Instantiate(colpo, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
Destroy(laser.gameObject, 4f);
Comment
Answer by DenisIsDenis · Nov 12, 2021 at 07:52 AM
You must assign a velocity relative to the bullet's creator object. For example, replace this:
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
with this:
laser.GetComponent<Rigidbody2D>().velocity = transform.up * projectileSpeed;
Hello Denis, done it and working, thank you very much, have an awesome day/day wherever you are bro