简单的镜头移动脚本

本文介绍了一种在Unity中实现游戏对象移动与视角控制的方法,包括使用WASD键进行移动,Shift键进行穿墙模式,Q/E键调整高度。通过自定义脚本MoveAndMouseLook,实现了对游戏对象的全方位控制,适用于场景浏览与游戏开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以直接放镜头或gameobject上,适合观看场景

Version 4:全改了

wasd/方向键:移动
shift:穿墙
Q/E:升降

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

public class MoveAndMouseLook : MonoBehaviour
{
    public Transform cam;

    public bool allowShiftWall = true;
    public bool allowSpaceFly = true;

    public float mouseXSpeed;
    public float mouseYSpeed;
    public float moveSpeed;
    private float speed;
    
    public float xAngle;
    public float yAngle;

    float mouseX;
    float mouseY;

    //默认有Rigibody 和 CapsuleCollider. 

    private void Start()
    {
        speed = moveSpeed;
        
    }
    private void Update()
    {
        mouseX = Input.GetAxisRaw("Mouse X") * mouseXSpeed * Time.deltaTime;
        mouseY = Input.GetAxisRaw("Mouse Y") * mouseYSpeed * Time.deltaTime;

        xAngle += mouseX;
        this.transform.eulerAngles = new Vector3(0, xAngle, 0);

        yAngle += mouseY;
        yAngle = Mathf.Clamp((yAngle % 360),130,230);
        //cam.eulerAngles = new Vector3(yAngle, 180, 180);
        cam.eulerAngles = new Vector3(yAngle, xAngle, 180);

        Vector3 move = new Vector3(-Input.GetAxis("Horizontal") * speed / 100, 0, -Input.GetAxis("Vertical") * speed / 100);
        if(move != Vector3.zero)
        {
            this.transform.position += Quaternion.Euler(0, xAngle, 0) * move;
        }

        if (allowSpaceFly && Input.GetKey(KeyCode.Q))
        {
            this.transform.position += Vector3.up;
        }
        else if (allowSpaceFly && Input.GetKey(KeyCode.E))
        {
            this.transform.position += Vector3.down;
        }

        if (allowShiftWall && Input.GetKeyDown(KeyCode.LeftShift))
        {
            speed = 10f;
            this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
            this.GetComponent<CapsuleCollider>().enabled = false;
        }
        if (allowShiftWall && Input.GetKeyUp(KeyCode.LeftShift))
        {
            speed = moveSpeed;
            this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
            this.GetComponent<CapsuleCollider>().enabled = true;
        }

    }
}


Version 3:基于Version2功能上简化了操作

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

public class MOVE : MonoBehaviour
{
    public Transform leftRotateCenter;
    public Transform rightRotateCenter;
    private Transform currentRotateCenter;

    public float speedLevel = 1;
    private float mySpeed;

    private void Update()
    {
        if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
        {
            currentRotateCenter = null;
            mySpeed = 0;
        }
        if (Input.GetMouseButtonDown(0))
        {
            currentRotateCenter = leftRotateCenter;
            mySpeed = -100;
        }
        if (Input.GetMouseButtonDown(1))
        {
            currentRotateCenter = rightRotateCenter;
            mySpeed = 100;
        }
        if (Input.GetKeyDown("-") && speedLevel > 0.5) 
        {
            speedLevel -= 0.5f;
        }
        if (Input.GetKeyDown("=") && speedLevel < 3)
        {
            speedLevel += 0.5f;

        }
    }

    private void LateUpdate()
    {
        if (currentRotateCenter != null)
        {
            this.transform.RotateAround(currentRotateCenter.position, Vector3.up, mySpeed * speedLevel * Time.deltaTime);
        }
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            if (Input.GetKey("space"))
            {
                transform.Translate(0, Input.GetAxis("Mouse ScrollWheel") * speedLevel, 0);
            }
            else
            {
                transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * speedLevel);
            }
        }
    }
}

Version 2:支持多种速度、分开了shift 和 space 的功能。

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

public class MOVE: MonoBehaviour
{
    public float horiSpeed = 10f;
    public float vertSpeed = 3f;
    public float speed = 2f;
    public float maxSpeed = 4f;
    public float minSpeed = 1f;

    private void Update()
    {
        if (Input.GetKeyUp("=") && speed < maxSpeed)
        {
            speed += 1;
        }
        if (Input.GetKeyUp("-") && speed > minSpeed)
        {
            speed -= 1;
        }
    }
    private void LateUpdate()
    {
        if (Input.GetKey("left shift")) 
        {
            var turnArround = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
            var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * (speed - 0.9f);
            transform.Rotate(0, turnArround, 0);
            transform.Translate(horizontal, 0, 0);
        }
        else if (Input.GetKey("space"))
        {
            var jump = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * 2 ;
            transform.Translate(0, jump, 0);
        }
        else
        {
            var horizontal = Input.GetAxis("Vertical") * Time.deltaTime * horiSpeed * speed;
            var vertical = Input.GetAxis("Horizontal") * Time.deltaTime * horiSpeed * speed;
            transform.Translate(horizontal, 0, vertical);
        }
    }
}

Version 1:space 功能是升降,shift 功能是减速

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

public class MOVE : MonoBehaviour
{
    public float fly = 10f;
    public float Vert = 20.0f;
    public float Hori = 100f;


    void Update()
    {
        if (Input.GetKeyUp("left shift"))
        {
            Vert = 20f;
            Hori = 100f;
            fly = 10f;
        }
        if (Input.GetKeyDown("left shift"))
        {
            Vert = 4f;
            Hori = 20f;
            fly = 2f;

        }
        
        if (Input.GetKey("space"))
        {
            var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
            var x = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
            transform.Translate(0, x, 0);
            transform.Rotate(0, y, 0);
        }
        else
        {
            var y = Input.GetAxis("Horizontal") * Time.deltaTime * Hori;
            var z = Input.GetAxis("Vertical") * Time.deltaTime * Vert;
            transform.Translate(y, 0, z);
        }
}


镜头围绕物体脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值