unity – Why when clamping the head rotation the head is jumping nonstop to the start rotation and back on the left side and up?


When I move the target to the left the head is like “jumping” to the start original rotation natural looking forward and back to the left side where the target is same behaviour when moving the target up.

and also the head is look backward when the target is at the back and it should not.

moving the target to the right and down and back move the head smooth but left,up,back not working good.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeadRotator : MonoBehaviour
{
    public Transform target;
    public Transform head;
    public float speed;

    public float minimumX;
    public float maximumX;

    public float minimumY;
    public float maximumY;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        Quaternion OriginalRot = head.rotation;
        head.LookAt(target);
        Quaternion NewRot = head.rotation;
        head.rotation = OriginalRot;
        head.rotation = Quaternion.Lerp(head.rotation, NewRot, speed * Time.deltaTime);



        float ry = head.eulerAngles.y;
        if (ry >= 180) ry -= 360;
        head.eulerAngles = new Vector3(
            Mathf.Clamp(head.eulerAngles.x, minimumX, maximumX),
            Mathf.Clamp(ry, minimumY, maximumY),
            0
        );

    }
}

I set in the inspector all min and max on x and y to -100 and 100



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email