Unity 跟随鼠标旋转

public class RotateScript : MonoBehaviour

{

    // Start is called before the first frame update

    public float speed = 1;

    private Vector3 lastPostion;

    public LayerMask whatIsGround;

    void Start()

    {


    }

    // Update is called once per frame

    void Update()

    {

        //旋转方法一

        //if (Input.GetMouseButtonDown(0))

        //{

        //    lastPostion = Input.mousePosition;

        //}

        //else if (Input.GetMouseButton(0)) {

        //    Vector3 newPostion = Input.mousePosition;

        //    Vector3 potPoint = lastPostion - newPostion;

        //    lastPostion = newPostion;

        //    Quaternion rot = Quaternion.Euler(potPoint.y* speed, potPoint.x * speed,0);

        //    transform.rotation = rot * transform.rotation;

        //}

        //旋转方法二

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        Debug.Log(ray);

        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo, 200, whatIsGround)) {

            Vector3 target = hitInfo.point;

            //target.y = transform.position.y;

            transform.LookAt(target);

        }

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容