Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hibano23 · Aug 04, 2018 at 01:51 AM · gameobjectinstantiateforceangle

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);
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Trying to Clone 1 of 5 GameObjects in a array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges