- Home /
Trouble with getting an instantiated object to travel at an angle using force
Hello Unity community. I am working on my first project using Unity2D so I am new and learning!. I have a player that when a "multishot" powerup is picked up, I want the bullets to fire a 3-way shot: one horizontal, one 30 degrees up, and one 30 degrees down. I had originally done this project in 3D and then converted over to 2D after realizing that is where I should be making this. The code for multishot worked before but now it doesn't. I've also tested this on a regular game object and it rotates the object and then applies the force to make it move at 30 degrees. I'm very confused as to why this is not working and after spending many, many hours trying to figure this out, I am at a loss. Also, I've tried creating a physics 2D material and changing friction to zero, thinking the sprites were sticking together. FYI, the cloned bullets do rotate but still continues to fire horizontally (not at 30 degrees). All 3 clones travel to the right together, one is horizontal, one rotated +30 deg, and the other rotated -30 deg. I've also tried AddRelativeForce and that did not work. The code below shows my multishot method. It seems to me that the force is still being applied globally and not relative to the local coordinate system of the rotated object. All the research I've done and the small test I did says this should be working. If anyone can give me insight into this, I'd really appreciate it.
private void FireMultishot()
{
StartCoroutine(Cooldown());
//Play the sound when the bullet is fired.
AudioSource.PlayClipAtPoint(fireBulletSound, Camera.main.transform.position);
GameObject Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
GameObject Temporary_Bullet_Handler1 = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation);
GameObject Temporary_Bullet_Handler2 = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
//Rotate the top and bottom bullets by 30 degrees.
Temporary_Bullet_Handler1.transform.Rotate(Vector3.forward * 30);
Temporary_Bullet_Handler2.transform.Rotate(Vector3.forward * -30);
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
Temporary_Bullet_Handler.GetComponent<Rigidbody2D>().AddForce(transform.right * Bullet_Forward_Force);
Temporary_Bullet_Handler1.GetComponent<Rigidbody2D>().AddForce(transform.right * Bullet_Forward_Force);
Temporary_Bullet_Handler2.GetComponent<Rigidbody2D>().AddForce(transform.right * Bullet_Forward_Force);
//Basic Clean Up, set the Bullets to self destruct after 3 Seconds.
Destroy(Temporary_Bullet_Handler, 3.0f);
Destroy(Temporary_Bullet_Handler1, 3.0f);
Destroy(Temporary_Bullet_Handler2, 3.0f);
}
Answer by Hibano23 · Aug 06, 2018 at 05:59 PM
I found the answer to my question. The issue is with this line (well the 3 lines):
Temporary_Bullet_Handler.GetComponent<Rigidbody2D>().AddForce(transform.right * Bullet_Forward_Force);
"transform.right" should be "Temporary_Bullet_Handler.transform.right". The former was not referencing the correct game object.
Your answer
Follow this Question
Related Questions
How to be notified of new GamObject creation? 1 Answer
Unwanted 2nd empty gameobject 1 Answer
2D Array of GameObjects... 1 Answer
adding force to instantiated ragdoll 4 Answers