Unity 通过键盘鼠标控制物体移动、旋转、缩放的方法

在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 update
    void Start()
    {
        
    }

    // Update is called once per frame
    void 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

猜你喜欢

转载自blog.csdn.net/mr_five55/article/details/134905588