Unity Scripts 学习笔记 - 002.摄像机全轴移动脚本

需求:运行状态下调整摄像机Transform;

代码块:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptOfTransformCameraMove : MonoBehaviour
{
    
    
    public float rotateSpeed = 50f;
    public float moveSpeed = 10f;

    void Update()
    {
    
    
        if (Input.GetKey("w"))
        {
    
    
            this.transform.Rotate(new Vector3(rotateSpeed * Time.deltaTime, 0, 0));
        }
        if (Input.GetKey("s"))
        {
    
    
            this.transform.Rotate(new Vector3(-rotateSpeed * Time.deltaTime, 0, 0));
        }
        if (Input.GetKey("a"))
        {
    
    
            this.transform.Rotate(new Vector3(0, rotateSpeed * Time.deltaTime, 0), Space.World);
        }
        if (Input.GetKey("d"))
        {
    
    
            this.transform.Rotate(new Vector3(0, -rotateSpeed * Time.deltaTime, 0), Space.World);
        }

        if (Input.GetKey("q"))
        {
    
    
            this.transform.Translate(new Vector3(0, moveSpeed * Time.deltaTime, 0), Space.World);
        }
        if (Input.GetKey("e"))
        {
    
    
            this.transform.Translate(new Vector3(0, -moveSpeed * Time.deltaTime, 0), Space.World);
        }
        if (Input.GetKey("right"))
        {
    
    
            this.transform.Translate(new Vector3(moveSpeed * Time.deltaTime, 0, 0), Space.World);
        }
        if (Input.GetKey("left"))
        {
    
    
            this.transform.Translate(new Vector3(-moveSpeed * Time.deltaTime, 0, 0), Space.World);
        }
        if (Input.GetKey("up"))
        {
    
    
            this.transform.Translate(new Vector3(0, 0, moveSpeed * Time.deltaTime), Space.World);
        }
        if (Input.GetKey("down"))
        {
    
    
            this.transform.Translate(new Vector3(0, 0, -moveSpeed * Time.deltaTime), Space.World);
        }
    }
}

代码块说明:

1.利用键盘上的 “W\A\S\D” 对摄像机的实现抬头,低头,转头操作;
2.利用键盘上的 “Q\E” 对摄像机进行升降操作;
3.利用键盘上的 “上下左右” 键对摄像机实现前后左右的位移操作;
整体实现多轴、多场景、全方位的需求;

猜你喜欢

转载自blog.csdn.net/m0_54122551/article/details/124654335