zoukankan      html  css  js  c++  java
  • uinty实现玩家尾随鼠标位置平滑旋转角度

    首先我们要在场景中加入一个quad平面作为地板, 然后指定Layer为Floor,然后移除mesh renderer组件

    然后加入脚本

    脚本主要思想是从屏幕中心投出一条射线到地板, 然后获取相应坐标,然后转化成角度

    最后然角色平滑转到该角度就可以

    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour
    {
        public float turnSmoothing = 5f;
        public float rayLength = 100f;
    
        private int floorMask; 
    
        void Awake()
        {
            floorMask = LayerMask.GetMask("Floor");
        }
    
        void FixedUpdate()
        {
            TurningWithMouse();
        }
    
        void TurningWithMouse()
        {
            Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit floorHit;
            if (Physics.Raycast(camRay, out floorHit, rayLength, floorMask))
            {
                Vector3 playerToMouse = floorHit.point - transform.position;
                playerToMouse.y = 0f;
                Quaternion targetRotation = Quaternion.LookRotation(playerToMouse, Vector3.up);
                Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
                rigidbody.MoveRotation(newRotation);
                
            }
        }
    }

  • 相关阅读:
    问题 I: 夫子云游
    问题 H: 小k的简单问题
    问题 G: 圆桌上的晚餐
    问题 F: 超超的自闭意思
    promise与aysnc 与EventProxy
    node的实践(项目三)
    node的实践(项目二)
    node不懂的方法的使用
    github
    node的实践(项目一)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6752011.html
Copyright © 2011-2022 走看看