- Home /
[Solved]How do i use the local axis on of child on parent?
I have a problem with a weapon in a 2D game I'm working with. The weapon do of course have recoil, and the recoil is in the opposite direction relative to where the weapon is pointing. This means that the recoil is local to the weapon which is a child of the player. The problem is that the weapon do not have a rigidbody and therefore i can't addforce to it and must addforce to the parent instead, but how can i addforce the weapons local x-axis on the parent's rigidbody?
'transform.right' of the child will the be local 'x' axis converted to world space (i.e. what AddForce() uses).
Answer by dizzy2003 · Aug 07, 2014 at 11:31 PM
To Turn a Vector3 from local space of one object to local space of another object.
First turn it into World space
Vector3 worldSpace=ObjChild.transform.TransformVector(ls);
Then turn it into local space of the other object
Vector3 ls2=ObjParent.transform.InverseTransformVector(worldSpace);
Thanks! This worked splendidly. I'm sorry I didn't specify which progra$$anonymous$$g language I use, but it wasn't that hard to find the corresponding commands in js. :)
Answer by zee_ola05 · Aug 07, 2014 at 11:57 PM
transform.position, transform.rotation returns the world position and rotation in world space. So you could just use that to calculate the recoil direction. Then you can apply the force in that direction to the parent's rigidbody.
Your answer
Follow this Question
Related Questions
Local 'forward' 3 Answers
Simulate rotation using two force thrusters 1 Answer
I Can't Assign Something To Local Space's X Axis 1 Answer
Moving set distance over set time in local space 1 Answer
Create variable based on local axis 1 Answer