unity用wasd控制前后左右,qe控制上下,鼠标控制视角转动

代码如下,unity用wasd控制前后左右,qe控制上下,鼠标控制视角转动,经常用到。


    public float speed = 1.0f;

    [HideInInspector]
    float xSpeed = 250.0f;
    float ySpeed = 120.0f;
    float yMinLimit = -20;
    float yMaxLimit = 80;
    private float x = 0.0f;
    private float y = 0.0f;  
    // Use this for initialization
    void Start () {
        var angles = transform.eulerAngles;  
        x = angles.y;  
        y = angles.x;  
    }
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
        {
            angle += 360;
        }
        if (angle > 360)
        {
            angle -= 360;
        }
        return Mathf.Clamp(angle, min, max);
    }  
    // Update is called once per frame
    void Update () {

        //w键前进  
        if (Input.GetKey(KeyCode.W))
        {
            this.gameObject.transform.Translate(new Vector3(0, 0,speed* Time.deltaTime), this.gameObject.transform);
        }
        //s键后退  
        if (Input.GetKey(KeyCode.S))
        {
            this.gameObject.transform.Translate(new Vector3(0, 0, -1 * speed * Time.deltaTime), this.gameObject.transform);
        }
        //a键后退  
        if (Input.GetKey(KeyCode.A))
        {
            this.gameObject.transform.Translate(new Vector3(-1 * speed * Time.deltaTime, 0, 0), this.gameObject.transform);
        }
        //d键后退  
        if (Input.GetKey(KeyCode.D))
        {
            this.gameObject.transform.Translate(new Vector3(Time.deltaTime * speed, 0, 0), this.gameObject.transform);
        }
        if (Input.GetKey(KeyCode.Q))
        {
            this.gameObject.transform.Translate(new Vector3(0, Time.deltaTime * speed, 0), this.gameObject.transform);
        }
        if (Input.GetKey(KeyCode.E))
        {
            this.gameObject.transform.Translate(new Vector3(0, -1 * Time.deltaTime * speed, 0), this.gameObject.transform);
        }
        if (Input.GetMouseButton(1))
        {
                x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

                y = ClampAngle(y, yMinLimit, yMaxLimit);

                var rotation = Quaternion.Euler(y, x, 0);
                transform.rotation = rotation;

        } 
    }
Unity中,你可以通过编写脚本来实现鼠标控制相机的旋转和键盘WASD控制相机的移动。首先,确保已经有一个游戏对象作为相机的主体,并且这个对象上绑定了Camera组件。 1. **鼠标控制相机旋转**: - 创建一个空的游戏对象作为Camera的Parent,设置它的Transform组件为"Rotation Mode"为"Quaternion"。 - 在Update()函数中,监听Input.GetAxis("Mouse X")和Input.GetAxis("Mouse Y"),这两个分别是鼠标在X轴和Y轴的偏移量,用于调整相机的水平和垂直视角变化。 - 使用`transform.Rotate()`方法,传入对应的偏移值和旋转轴(通常是Vector3.up和Vector3.right),即可实现鼠标滚轮的滚动控制相机的朝向。 ```csharp void Update() { float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); transform.Rotate(0, mouseY * sensitivity, 0); transform.Rotate(mouseX * sensitivity, 0, 0); } ``` 这里的`sensitivity`是一个自定义的缩放系数,用于调节旋转速度。 2. **键盘控制相机移动**: - 同样在Update()里,监听Input.GetKey(KeyCode.W), KeyCode.A, KeyCode.S, KeyCode.D对应的是WASD四个方向。 - 根据按键状态,计算出相机的平移距离,然后使用`transform.Translate()`方法: ```csharp float speed = 5f; // 移动速度 Vector3 moveDirection; if (Input.GetKey(KeyCode.W)) moveDirection += Vector3.forward; if (Input.GetKey(KeyCode.A)) moveDirection -= Vector3.right; if (Input.GetKey(KeyCode.S)) moveDirection -= Vector3.forward; if (Input.GetKey(KeyCode.D)) moveDirection += Vector3.right; moveDirection *= speed * Time.deltaTime; transform.Translate(moveDirection); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值