- Home /
Question by
FreeTimeDev · Jul 23, 2013 at 06:03 PM ·
rotationparentchildmathf.clamp
Clamping Child's Rotation Relative To Partent's
I have a 2d game where I'm attaching turrets (the children) to a space ship (parent).
Right now, when I disable the movement script for the parent my rotation script works fine; however, when the parent changes it's rotation the children's rotation becomes unreliable. I know that somehow I have to take the parents rotation into consideration but I don't know how.
using UnityEngine;
public class TurretRot : MonoBehaviour
{
public Transform player;
public float minZRot, maxZRot;
private float zRot = 0f;
public bool rightSide = true;
private Vector3 tmpEuler;
public float speed = 45f;
void Awake()
{
tmpEuler = transform.eulerAngles;
}
void Start()
{
if(rightSide)
{
minZRot = -180f;
maxZRot = 0f;
}else
{
minZRot = 0f;
maxZRot = 180f;
}
}
void Update ()
{
//Clamping Child's Rotation Relative To Partent's
if(rightSide)
{
//
}
zRot = -Mathf.Atan2(player.position.x - transform.position.x, player.position.y - transform.position.y) * (180 / Mathf.PI);
zRot = Mathf.Clamp(zRot, minZRot, maxZRot);
float angleZ = Mathf.MoveTowardsAngle(transform.localEulerAngles.z, zRot, speed * Time.deltaTime);
transform.localRotation = Quaternion.Euler(0,0,zRot);
}
}
Edit: rightSide tells the turret which side of the ship it's on. In case that wasn't obvious.
Comment
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Delay child objects rotation 0 Answers
Changing child's rotation changes parent's position 0 Answers
Parent-independent rotation 2 Answers
how do I get Parent rotation on Child,how do I use a parents rotation in a child? 2 Answers