- Home /
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?
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.
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
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
Follow this Question
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