- Home /
How do I get AddRelativeForce to work?
I have been trying to create a shotgun spread for a game. I'm using unity 2017 and this code doesn't work. the rotation works fine but the addrelativeforce just sends the objects forward instead of forward relative to their own axis. here is the code.
Vector3 playerPos = new Vector3(transform.position.x + 1, transform.position.y + .5f,
transform.position.z);
GameObject obj = Instantiate(prefab2, playerPos, Quaternion.identity);
obj.GetComponent<Transform>().Rotate(new Vector3(0, 0, 10));
obj.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(2000,0));
GameObject obj2 = Instantiate(prefab2, playerPos, Quaternion.identity);
obj2.GetComponent<Transform>().Rotate(new Vector3(0, 0, -10));
obj2.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(2000, 0));
GameObject obj3 = Instantiate(prefab2, playerPos, Quaternion.identity);
obj3.GetComponent<Transform>().Rotate(new Vector3(0, 0, 5));
obj3.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(2000, 0));
GameObject obj4 = Instantiate(prefab2, playerPos, Quaternion.identity);
obj4.GetComponent<Transform>().Rotate(new Vector3(0, 0, -5));
obj4.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(2000, 0));
GameObject obj5 = Instantiate(prefab2, playerPos, Quaternion.identity);
obj5.GetComponent<Transform>().Rotate(new Vector3(0, 0, 0));
obj5.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(2000, 0));
i rotate each bullet and then add a relative force but the bullets wont spread and stay straight. I have no idea whats wrong. I made a test file and did the same thing except the object was already spawned in and a key activated the rotation and force.
Your answer
Follow this Question
Related Questions
Getting the Bullet to add the Shooters speed to it's own 0 Answers
[C#]Instantiate projectile a distance away from Player 0 Answers
How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers
transform.TransformDirection not working -- WHAT IM MISSISNG? 1 Answer
Predict Collision 0 Answers