在Unity中,使用鍵盤ADWS鍵控制物體移動,通過鼠標左鍵控制物體旋轉,鼠標中鍵控制物體縮放是再常見不過的方法。
方法如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MoveController : MonoBehaviour
{float moveSpeed = 10f;float rotateSpeed = 1000f;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//獲取橫軸參數float Horizontal = Input.GetAxis("Horizontal");//獲取垂直參數float Vertical = Input.GetAxis("Vertical"); //鍵盤ADWS鍵控制物體移動。//通過乘于Time.deltaTime,就可以讓物體以每秒moveSpeed單位的速度向前移動transform.Translate(new Vector3(Horizontal * Time.deltaTime * moveSpeed, 0, Vertical * Time.deltaTime * moveSpeed)); //左鍵鼠標點擊狀態下移動鼠標旋轉if(Input.GetMouseButton(0)){//通過獲取鼠標XY軸移動數值控制物體旋轉transform.Rotate(new Vector3(Input.GetAxis("Mouse X") * Time.deltaTime * rotateSpeed, Input.GetAxis("Mouse Y") * Time.deltaTime * rotateSpeed));}//通過獲取鼠標中鍵滑動值控制物體縮放transform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");}
}
?效果如下:Unity 通過鍵盤鼠標控制物體移動、旋轉、縮放_嗶哩嗶哩_bilibili