在Unity中,使用鍵盤ADWS鍵控制物體移動,通過鼠標(biāo)左鍵控制物體旋轉(zhuǎn),鼠標(biāo)中鍵控制物體縮放是再常見不過的方法。
方法如下:文章來源:http://www.zghlxwxcb.cn/news/detail-776213.html
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 update
void Start()
{
}
// Update is called once per frame
void Update()
{
//獲取橫軸參數(shù)
float Horizontal = Input.GetAxis("Horizontal");
//獲取垂直參數(shù)
float Vertical = Input.GetAxis("Vertical");
//鍵盤ADWS鍵控制物體移動。
//通過乘于Time.deltaTime,就可以讓物體以每秒moveSpeed單位的速度向前移動
transform.Translate(new Vector3(Horizontal * Time.deltaTime * moveSpeed, 0, Vertical * Time.deltaTime * moveSpeed));
//左鍵鼠標(biāo)點(diǎn)擊狀態(tài)下移動鼠標(biāo)旋轉(zhuǎn)
if(Input.GetMouseButton(0))
{
//通過獲取鼠標(biāo)XY軸移動數(shù)值控制物體旋轉(zhuǎn)
transform.Rotate(new Vector3(Input.GetAxis("Mouse X") * Time.deltaTime * rotateSpeed, Input.GetAxis("Mouse Y") * Time.deltaTime * rotateSpeed));
}
//通過獲取鼠標(biāo)中鍵滑動值控制物體縮放
transform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");
}
}
?效果如下:Unity 通過鍵盤鼠標(biāo)控制物體移動、旋轉(zhuǎn)、縮放_嗶哩嗶哩_bilibili文章來源地址http://www.zghlxwxcb.cn/news/detail-776213.html
到了這里,關(guān)于Unity 通過鍵盤鼠標(biāo)控制物體移動、旋轉(zhuǎn)、縮放的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!