Hi
My problem here is with the child object that rotate with the main object. the main object rotate on Y axis only but the child use X axis only. but it doesn’t work like this. the whole object rotate on Y & X axis !!
Here is my script:
public float z_angle;
public float x_angle;
public float min_x,max_x;
public float min_z,max_z;
public Transform target;
public float dist;
public Transform base1; // child
// Update is called once per frame
void Update () {
dist = Vector3.Distance(gameObject.transform.position, target.transform.position);
if (dist <= 50) {
Vector3 charAngle = gameObject.transform.eulerAngles;
charAngle.z = (charAngle.z > 180) ? charAngle.z-360 : charAngle.z;
charAngle.z = Mathf.Clamp(charAngle.z, -min_z, max_z);
z_angle = charAngle.z;
gameObject.transform.rotation = Quaternion.Euler(charAngle);
Vector3 charAnglex = base1.transform.eulerAngles;
charAnglex.x = (charAnglex.x > 180) ? charAnglex.x-360 : charAnglex.x;
charAnglex.x = Mathf.Clamp(charAnglex.x, -min_x, max_x);
x_angle = charAnglex.x;
base1.transform.rotation = Quaternion.Euler(charAnglex);
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 2.5f);
}
}
