- Home /
Mouselook script beserk when GO made child
I've got a mouselook script that I'm relatively happy with, to aim a cannon. This all works fine if the cannon GameObject has no parent.
However, when I make the cannon the child of another gameObject, this happens:
As you can see, not only the Y rotation is affected, but the X and Z as well. The code is as follows:
void Update ()
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray hitRay = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDist = 0.0f;
if(playerPlane.Raycast(hitRay,out hitDist))
{
Vector3 targetPoint = hitRay.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
Vector3 tempAngles = new Vector3(targetRotation.eulerAngles.x,ClampAngle(targetRotation.eulerAngles.y, maxAngle * -1, maxAngle), targetRotation.eulerAngles.z);
targetRotation.eulerAngles = tempAngles;
transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation, 1);
}
}
public static float ClampAngle(float angle, float min, float max)
{
if(angle > 180f)
{
angle -= 360f;
}
angle = angle % 180;
return Mathf.Clamp(angle, min, max);
}
The thought that just occurred to me is this: does a child object take values from its parent's transform? Because as I said, the X and Z axes are being affected, where they weren't before.
I'm completely flummoxed by this, any advice to get me started on troubleshooting would be awesome.
Your answer
Follow this Question
Related Questions
Translate.transform problems (collision/Rigibody) 0 Answers
Transform not changing to correct target transform 0 Answers
Shrinking an Object 1 Answer
C# 2d Always Rotate Facing Gameobject 1 Answer
Error code CS0029. 1 Answer