Unity3d 控制物体移动、旋转、缩放
在Unity中通过利用 Input Manager(输入管理器)可以很简单的实现对一个物体进行移动、旋转、缩放操作。
演示代码:
//通过虚拟轴控制物体移动、旋转、缩放 public class MoveCube : MonoBehaviour { float moveSpeed = 10f; float rotateSpeed = 50f; void Update() { float horizontal = Input.GetAxis("Horizontal"); //获取横轴参数。(如:按A键返回-1 按D键返回+1,但中间有一个过度,而不是直接变-1或+1。) float vertical = Input.GetAxis("Vertical"); //获取垂直参数。 Debug.Log("horizontal: " + horizontal + ",vertical:" + vertical); float mouseX = Input.GetAxis("Mouse X");//获取鼠标X轴滑动参数 float mouseY = Input.GetAxis("Mouse Y");//获取鼠标Y轴滑动参数 Debug.Log("mouseX: " + mouseX + ",mouseY:" + mouseY); float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel"); //键盘ADWS键控制物体移动。(当乘以Time.deltaTime实际表示:每秒移动物体N米,而不是每帧N米。) this.transform.Translate(new Vector3(horizontal * Time.deltaTime * moveSpeed, 0, vertical * Time.deltaTime * moveSpeed)); //鼠标XY轴滑动控制物体旋转 this.transform.Rotate(new Vector3(mouseX * Time.deltaTime * rotateSpeed, mouseY * Time.deltaTime * rotateSpeed)); //鼠标中键控制物体缩放 this.transform.localScale += new Vector3(mouseScrollWheel, mouseScrollWheel, mouseScrollWheel); } }