Parent object to face childs rotation...

HI :slight_smile: Please tell me if i have explained this badly and i will re-write!

  • 3rd person perspective game

  • I’ve got 2 objects, a head and a body, the head is a child of the body

  • When i move the mouse to look around, the head moves, and is always looking toward the center of the screen

  • when the head moves 35 degrees from the direction of the body, i want the body to turn in the same direction by 35 degrees, so that it lines back up with the head

  • The code i wrote doesn’t seem to work this way, instead, it will work once and then the body will continually follow the head until the head centralises, at which point it works again

  • can you smart cats take a look and advise my dumb ass?

    var body : GameObject;

    var turnSpeed : float = 1.0;

    var turnTrigger : float = 35;

    var lookSensitivity : float = 5.0;

    var yRotation : float;

    var xRotation : float;

    var currentYRotation : float;

    var currentXRotation : float;

    var yRotationV : float;

    var xRotationV : float;

    var lookSmoothDamp : float = 0.1;

    function Update () {

    var bodyRotation = body.transform.localRotation.y;

     yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
     xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
     
     xRotation = Mathf.Clamp(xRotation, -90, 45);
     
     //HERE'S THE CODE I'M HAVING TROUBLE WITH:
     
     if (yRotation > bodyRotation + turnTrigger){
     body.transform.eulerAngles.y = yRotation;
    
     Debug.Log("turn right!");}
     
     if (yRotation < bodyRotation - turnTrigger){
     body.transform.eulerAngles.y = yRotation;
    
     Debug.Log("turn left!");}
     
     //THAT'S IT!
     
    
     currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
     currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, xRotationV, lookSmoothDamp);
    
    
     transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
    

    }

The Code is on the ‘head’ object.

I only wrote the code which turns the body, the ‘mouse look’ script is from ETeeski Tutorials.

Thanks, Tom :slight_smile:

The problem (I believe) is that you are trying to set an individual axis through the eulerAngles property. This is a no-no. Individual axes can be -read- with the eulerAngles.y value, but should only be set with a full new vector (transform.eulerAngles)

try:

body.transform.eulerAngles = Vector3(body.transform.eulerAngles.x, yRotation, body.transform.eulerAngles.z);