【unity学习个人纪录】鼠标点击拖拽物体旋转
·
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//鼠标点击拖拽旋转
public class MouseOnClickRotate : MonoBehaviour
{
public GameObject tarGetObj;//拖拽需要旋转的物体
[Range(5, 50)] public float rotateSpeed = 150f;//旋转速度
private Vector3 lastPosition;//纪录鼠标位置
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
lastPosition = Input.mousePosition;
//鼠标上一帧位置
}
else if (Input.GetMouseButton(0))
{
Vector3 nowPosition = Input.mousePosition;
//鼠标当前位置
Vector3 posDelta = lastPosition - nowPosition;
//位置差
lastPosition = nowPosition;
//并更新上一帧位置
Quaternion rot = Quaternion.Euler(posDelta.y * rotateSpeed * Time.deltaTime,
posDelta.x * rotateSpeed * Time.deltaTime, 0);
//角度增加的量
tarGetObj.transform.rotation = rot* transform.rotation;
//旋转(位置更新)
}
}
}
更多推荐


所有评论(0)