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);

    }
}
相关推荐
点心的游戏开发世界2 小时前
GDScript 入门笔记(五):函数
笔记·游戏引擎·godot
AomanHao2 小时前
【ISP】compression artifact压缩失真情况
图像处理·数码相机·isp·sensor·图像压缩·压缩失真
谢斯4 小时前
[unity]如何在unity中添加newtonsoft-json
unity·json·游戏引擎
蜡台5 小时前
ux-camera 跨平台相机组件
数码相机·vue·uniapp·camera·nvue·取景框
天人合一peng6 小时前
Unity标记固定颜色及投影标记
unity
云雨巫山9 小时前
Unity 游戏开发性能优化全景手册
unity·智能手机·性能优化·游戏引擎
maybeyaluokenai9 小时前
unity build in渲染管线概述
unity·图形渲染
学习星球9 小时前
AI 原生游戏开发:Godot 4 + AI Agent 全栈指南(Ziva 3 / Godot MCP 深度实战)
人工智能·游戏引擎·godot
云雨巫山9 小时前
Unity 性能优化 · 图解全景手册
unity·性能优化·游戏引擎
长江后浪博客10 小时前
YOLOv26+500万彩色工业相机实现药品铝塑板颗粒完整度检测:缺粒识别、颜色检测与穹顶漫射光机器视觉方案
数码相机·yolo·机器视觉·yolov26·药品包装检测·铝塑板检测·彩色工业相机