Unity使用脚本控制相机移动、旋转

考虑到以后可能经常需要用到这个功能,所以写篇博客记录下代码。我的代码参考自博客:https://www.cnblogs.com/forever3329/p/17798070.html

功能:键盘wasd控制前后左右平移,qe控制左右视角旋转,rf控制视角升降。


csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Initialize_fruit : MonoBehaviour
{
    // Start is called before the first frame update
    //相机移动
    public int panSpeed;
    public float rotationAmount;
    public Vector3 zoomAmount;
    public int moveTime;
    private GameObject mainCamera;

    void Start()
    {
        //相机移动
        panSpeed = 1;
        rotationAmount = 0.1f;
        mainCamera = GameObject.Find("Main Camera");
    }

    // Update is called once per frame
    void Update()
    {
        CameraMove(mainCamera);
    }

    void CameraMove(GameObject camera)
    {//wasd控制前后左右平移,qe控制左右视角旋转,rf控制视角升降。
        var newPos = new Vector3(0, 0, 0);
        Vector3 newRotation = new Vector3(0, 0, 0);
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
        {
            Debug.Log("W or up");
            newPos = camera.transform.forward * panSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
        {
            Debug.Log("S or down");
            newPos = -camera.transform.forward * panSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
        {
            Debug.Log("D or right");
            newPos = camera.transform.right * panSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
        {
            Debug.Log("A or left");
            newPos = -camera.transform.right * panSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.Q))
        {
            Debug.Log("Q");
            newRotation = Vector3.up * rotationAmount;
        }

        if (Input.GetKey(KeyCode.E))
        {
            Debug.Log("E");
            newRotation = Vector3.down * rotationAmount;
        }
        if (Input.GetKey(KeyCode.R))
        {
            Debug.Log("R");
            newPos = Vector3.up * panSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.F))
        {
            Debug.Log("F");
            newPos = Vector3.down * panSpeed * Time.deltaTime;
        }
        camera.transform.position += newPos;
        camera.transform.rotation = camera.transform.rotation * Quaternion.Euler(newRotation);

    }
}
相关推荐
快乐觉主吖10 小时前
Unity网络通信的插件分享,及TCP粘包分包问题处理
tcp/ip·unity·游戏引擎
格林威20 小时前
Baumer工业相机堡盟工业相机如何通过YoloV8模型实现人物识别(C#)
开发语言·人工智能·数码相机·yolo·计算机视觉·c#
sunly_21 小时前
Flutter:上传图片,选择相机或相册:wechat_assets_picker
数码相机·flutter·微信
Cedric11131 天前
不同相机CMOS噪点对荧光计算的影响
数码相机
moonsims1 天前
高光谱相机(Hyperspectral Camera)
数码相机
中达瑞和-高光谱·多光谱1 天前
高光谱相机有多少种类型?分别有什么特点?
数码相机
erxij2 天前
【游戏引擎之路】登神长阶(十八):3天制作Galgame引擎《Galplayer》——无敌之道心
游戏引擎
lizz312 天前
GAMES101 lec2-数学基础1(线性代数)
线性代数·游戏引擎·图形渲染
啊基米德2 天前
lua(xlua)基础知识点记录一
unity·lua·xlua
夜色。2 天前
Unity Android Logcat插件 输出日志中文乱码解决
android·unity