旋转播放器头时,如何将旋转方向的每一侧夹紧到180度或设置为自己的夹紧度?

问题描述

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

public class Interactable : MonoBehaviour
{
    public Transform objToRotateLookAT;
    public Transform body;
    public float lookAtSpeed;
    public float originalLookSpeed;
    public float rotationSpeed;
    public float distanceToInteract;
    public Animator anim;
    public GameObject text;

    private bool raycastSucceed;
    private Vector3 originalRotation;
    private bool originalRotFlag = true;
    private bool isInstant = false;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = false;

        originalRotation = objToRotateLookAT.position;

        if (anim == null)
        {
            anim = GetComponent<Animator>();
        }
    }

    void FixedUpdate()
    {
        int layerMask = 1 << 8;

        RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),out hit,Mathf.Infinity,layerMask))
        {
            if (!raycastSucceed)
                Debug.Log("Did Hit");
            raycastSucceed = true;
            Debug.DrawRay(transform.position,transform.TransformDirection(Vector3.forward) * hit.distance,Color.red);

            text.SetActive(true);

            if (Vector3.Distance(transform.position,hit.transform.position) <= distanceToInteract)
            {
                anim.SetBool("Pickup Item",true);
            }
        }
        else
        {
            if (raycastSucceed)
                Debug.Log("Did not Hit");
            raycastSucceed = false;
            Debug.DrawRay(transform.position,transform.TransformDirection(Vector3.forward) * 1000,Color.yellow);

            text.SetActive(false);

            if (originalRotFlag == false)
            {
               
            }
            else
            {
               
            }
        }
    }

    private void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Quaternion targetRotation = Quaternion.LookRotation(ray.direction);


        if (isInstant)
        {
            objToRotateLookAT.rotation = targetRotation;
        }
        else
        {
            Quaternion currentRotation = objToRotateLookAT.rotation;

            float angularDifference = Quaternion.Angle(currentRotation,targetRotation);
            // will always be positive (or zero)

            

            if (angularDifference > 0) objToRotateLookAT.rotation = Quaternion.Slerp(
                                         currentRotation,targetRotation,(rotationSpeed * 180 * Time.deltaTime) / angularDifference
                                    );
            else objToRotateLookAT.rotation = targetRotation;
        }
    }
}

在更新中,我使用鼠标光标位置旋转图层头。 问题是我可以将头部向后旋转,因此在某些情况下,当头部向后时,身体会保持面向前。

我想做一些夹紧,这样头部才能像人的真实头部旋转极限一样左右旋转。

我只是不确定在更新中如何以及在何处进行钳制。

运行游戏时的屏幕截图,头部和身体都朝着相同方向:

Same direction

但是如果我过多地移动鼠标并旋转头部,则:

The head is facing backward and the body forward.

我尝试了一下,但是没有起作用,试图测试targetRotation和currentRotation的钳位以将其限制为-9和9,但是没有任何变化:

private void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Quaternion targetRotation = Quaternion.LookRotation(ray.direction);

        if (isInstant)
        {
            objToRotateLookAT.rotation = targetRotation;
        }
        else
        {
            Quaternion currentRotation = objToRotateLookAT.rotation;

            targetRotation.y = Mathf.Clamp(targetRotation.y,-9f,9f);
            targetRotation.x = Mathf.Clamp(targetRotation.x,9f);
            targetRotation.z = Mathf.Clamp(targetRotation.z,9f);

            currentRotation.y = Mathf.Clamp(currentRotation.y,9f);
            currentRotation.x = Mathf.Clamp(currentRotation.x,9f);
            currentRotation.z = Mathf.Clamp(currentRotation.z,9f);

            float angularDifference = Quaternion.Angle(currentRotation,targetRotation);
            // will always be positive (or zero)

            if (angularDifference > 0) objToRotateLookAT.rotation = Quaternion.Slerp(
                                         currentRotation,(rotationSpeed * 180 * Time.deltaTime) / angularDifference
                                    );
            else objToRotateLookAT.rotation = targetRotation;
        }
    }

这是我录制的一个简短视频剪辑的链接,显示了当立方体在播放器后面时,并且播放器头部的旋转速度与立方体的速度不同的问题,因此在大多数情况下,播放器不看在立方体上,但从大方向看:

https://www.youtube.com/watch?v=gApXlKv7hKY&feature=youtu.be

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...