Unity3D 物体跟随鼠标旋转

时间:2022-02-19 10:20:47

float speed=1000f;

void Update ()
{

if (Input.GetMouseButton (0)) {
transform.Rotate (0,-Input.GetAxis ("Mouse X") * Time.deltaTime * speed,0);
}

}

 

让UI上面的3D模型根据鼠标拖动绕Y轴旋转。

 

后面发现一个问题:两个物体同时放到UI界面的时候,会同时跟着鼠标转动。后面添加了OnMouseDown()来解决这个问题;

public float speed = 1000f;
private bool isTouch = false;

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

if (Input.GetMouseButton (0) && isTouch) {

transform.Rotate (0, -Input.GetAxis ("Mouse X") * Time.deltaTime * speed, 0);
}
if (Input.GetMouseButtonUp (0)) {
isTouch = false;
}

}

public void OnMouseDown ()
{
isTouch = true;

}