Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 danhanly · Aug 09, 2012 at 12:41 PM · instantiatetransformaddforceaddrelativeforce

AddRelativeForce not adding force relatively

I Instantiate a 'Jet' at runtime and I would like to project it from a launcher.

My 'Root' object is my launcher.

    function Start(){
     initialJetCoords = Vector3(-2.062895, -4.464173,-0.1031868);
     root = GameObject.Find("Root");
     theJet = GameObject.Instantiate(jet,initialJetCoords,Quaternion.identity);
     theJet.transform.parent = GameObject.Find("Root").transform;
     theJet.transform.localPosition = initialJetCoords;
     theJet.transform.rotation = Quaternion.identity;
    }
I make the Jet a child of the launcher, so the launcher can move freely. Then I reset the Jet's coordinates to ensure it's in the right position.

The jet currently gets created at the correct position and rotation.

This method happens on a button press:

 function AddForce () {
  theJet.rigidbody.AddRelativeForce(Vector3.up*100000);
 }

The issue I'm having is that If the launcher is rotated, the Jet still appears to obey Global coordinates and not local to the 'root' launcher.

Isn't that what AddRelativeForce is supposed to do?

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

2 Replies

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

Answer by danhanly · Aug 09, 2012 at 03:54 PM

I feel stupid.

Basically, my issue wasn't that it wasn't taking off in the correct direction, it was that it's rotation wasn't fixed.

I fixed the rotation here:

 function Start(){
      initialJetCoords = Vector3(-2.062895, -4.464173,-0.1031868);
      root = GameObject.Find("Root");
      theJet = GameObject.Instantiate(jet,initialJetCoords,Quaternion.identity);
      theJet.transform.parent = GameObject.Find("Root").transform;
      theJet.transform.localPosition = initialJetCoords;
      theJet.transform.localRotation = Quaternion.identity; //I NOW USE LOCALROTATION INSTEAD OF ROTATION
     }

The use of localRotation over just rotation means that it's rotation matrix is always correct.

Another issue I had was that an object within my launcher was affecting it's identity; i.e. when the object made contact with the jet before launching, it's origin and rotation changed.

My jet was made from a sphere until I get some tidy models made, so I couldn't see this change in rotation.

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
avatar image
0

Answer by aldonaletto · Aug 09, 2012 at 02:06 PM

AddRelativeForce applies the force relative to the local coordinates, thus the jet should go in its local up direction when the force is applied - if the jet rotates after that, the movement will continue in the original direction. Maybe you're having problems with the parent/child relationship: since you've childed the jet to the launcher, any launcher rotation/movement after firing will affect the jet as well, producing very weird results. You could detach the jet when firing, like this:

 theJet.transform.parent = null;
 AddForce(); // now the jet is independent of the launcher
Comment
Add comment · Show 1 · 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
avatar image danhanly · Aug 09, 2012 at 02:19 PM 0
Share

Hi Aldo, It's the initial 'take-off' that points in the wrong direction. If I shoot a jet, it comes out of the launcher at the correct point; if I the turn the launcher 45 degrees, and shoot again (which Instantiates my jet prefab in the identical way as in my Start() function above), the jet shoots from the same point (i.e. 45 degrees away from where it's supposed to be).

It always obeys Global 'up' and not local 'up' despite being a child of the launcher and using AddRelativeForce.

Dan

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

8 People are following this question.

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

Related Questions

create a cannon ball based on cannon angles and cannon power 0 Answers

Facing the target 0 Answers

Rigid body rotation question 1 Answer

Can't remove instantiated prefab 0 Answers

transform bullitprefab errors 0 Answers


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