- Home /
Question is off-topic or not relevant
Child object not rotating as parent
I have a FOV triangle as a child to a kind of homing missile. When it sees an enemy it is supposed to chase that enemy.
At instantiation the rotation is set to the powerup and starts out correctly. However when I start moving on my 3D track where you could go any way the FOV keeps looking the same way even though the weapon keeps its correct path accross the track. The FOV keeps the correct position relative to the weapon.
In my FOV script
void Update()
{
transform.position = parent.position;
transform.rotation = parent.rotation;
}
This helps for the position, but not for the rotation.
Debugging the rotation tells me the object isn't turning, yet it has to, to keep going on the track.
I use below code to keep the weapons to the track, which tells me the weapon should be rotating else it would go off into space...
// stick the powerup to the track
if (distForward < distDown && distForward < distBack)
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitForward.normal), hitForward.normal), Time.deltaTime * 5.0f);
else if (distDown < distForward && distDown < distBack)
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitDown.normal), hitDown.normal), Time.deltaTime * 5.0f);
else if (distBack < distDown && distBack < distForward)
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitBack.normal), hitBack.normal), Time.deltaTime * 5.0f);
// apply gravity
if (distForward < Mathf.Infinity || distDown < Mathf.Infinity || distBack < Mathf.Infinity)
rigidbody.AddForce(-transform.up * Time.deltaTime * localGravityForce);
So how to make sure the FOV triangle gets the rotation needed to keep looking forward relative to the weapon?
I'm not sure I get your question right, but anyways, if the FOV is a child of the missile why do you set the position and rotation manually?
because without it's not moving with the parent object...
rotation wise it isn't doing anything with or without...
maybe that is because the actual weapon has a collider and everything and the FOV is "just" a trigger
If an object is a child of another object then, unless it has a rigidbody, it will move(update its position) and rotate with the parent without the need for any code. So it could be that as you are trying to code this rather than letting it do this automatically that is causing your problem. Try commenting out the two lines of code in your Update() above and see if that makes it work.
Check that the child and parent have a uniform Scale of (1, 1, 1), this too can affect movement etc in a unwanted way if not.
If it does have a rigidbody and the parent also has a rigidbody then get rid of the rigidbody on this(the child) and see if that helps.
then there's the problem... it has a rigidbody
The reason it has a rigidbody is because the parentclass is required to have a rigidbody and the FOV script inherits from this parentclass. $$anonymous$$ost importantly to be able to set the target (when found).
edit: removing the rigidbody and the position and rotating update gives me a FOV that stays in front of the weapon, but it still doesn't rotate... like the weapon isn't rotating.
Can you upload a couple of pics to demonstrate exactly what is happening, and of the object hierarchy(fully expanded), and the inspector view of the parent and child components. Could you also post the full FOV script. Thanks.
Answer by Baste · Dec 15, 2014 at 11:38 AM
The child object IS rotating. The rotation shown in the inspector is rotation relative to the parent. The same goes for position and scale.
A child transform always moves and rotates with it's parent transform. If the parent moves, the child moves as far in the same direction. If the parent rotates, all the children objects rotates around the parent, staying at the same relative position.
When you do this:
transform.position = parent.position;
transform.rotation = parent.rotation;
You're centering the child transform at the parent's position, and giving it the same rotation as the parent - meaning that it's local position and rotation should show as 0. On additional frames beyond the first one, your Update does nothing that's not already happening.
If it helps to clarify, this Update function is identical to your Update:
void Update() {
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
}
I would like to think so indeed... however it doesn't...
When not doing that in update the FOV triangle just stays at it's instantiated position.
probably because a rigidbody.
edit: removing the rigidbody and the position and rotating update gives me a FOV that stays in front of the weapon, but it still doesn't rotate... like the weapon isn't rotating.
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Smooth Follow Camera Rotate on Z-Axis? 2 Answers
Change rotation axis when importing from Maya 2 Answers
C# Need Help Refining Ricochet Script 1 Answer
C# GameObject Reverses Z Rotation 0 Answers