Unity3d中控制摄像机运动

该脚本实现了在Unity3D中使用键盘和鼠标控制摄像机移动和旋转的功能。通过MonoBehaviour类,Update方法更新摄像机状态。鼠标右键旋转,中键平移,W/S/A/D键控制前后左右移动,Q/E键上下移动,同时支持Shift键加速移动。
摘要由CSDN通过智能技术生成

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

///
/// 用户控制摄像机实现移动旋转功能点
///
public class CameraMove : MonoBehaviour
{

private Vector3 m_camRot;
private Transform m_camTransform;
private float m_rotateSpeed = 0.5f;

public float m_speed = 0.2f;    //初始移动速度
private float moveSpeed = 0.15f; //移动速度


//记录运动加速度
float x_m;
float y_m;
float z_m;
float d;

// Start is called before the first frame update
void Start()
{
    m_camTransform = Camera.main.transform;
    m_camRot = Camera.main.transform.eulerAngles;
}

// Update is called once per frame
void Update()
{
    PlayerMove();
    CameraRotate_Mouse();
}

/// <summary>
/// 用途:相机跟随鼠标旋转
/// </summary>
void CameraRotate_Mouse() 
{
    if (Input.GetMouseButton(1)) 
    {
        //获取鼠标的移动距离
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");
        //旋转相机
        m_camRot.x -= rv * m_rotateSpeed;
        m_camRot.y += rh * m_rotateSpeed;
    }

    m_camTransform.eulerAngles = m_camRot;
    if (Input.GetMouseButton(2)) 
    {
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");

        //赋值
        transform.Translate(transform.up * -rv * m_rotateSpeed + transform.right * -rh * m_rotateSpeed, Space.World);
    }
}


void PlayerMove() 
{
    if (Input.GetKey(KeyCode.LeftShift))
    {
        moveSpeed = m_speed * 3;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift)) 
    {
        moveSpeed = m_speed;
    }

    if (Input.GetKey(KeyCode.W))
    {
        z_m = z_m + Time.deltaTime * moveSpeed;
        transform.Translate(transform.forward * z_m, Space.World);
    }
    else if (Input.GetKeyUp(KeyCode.W)) 
    {
        z_m = 0;
    }


    if (Input.GetKey(KeyCode.S))
    {
        z_m = z_m - Time.deltaTime * moveSpeed;
        transform.Translate(transform.forward * z_m, Space.World);
    }
    else if (Input.GetKeyUp(KeyCode.S)) 
    {
        z_m = 0;
    }

    if (Input.GetKey(KeyCode.A))
    {
        x_m = x_m - Time.deltaTime * moveSpeed;
        
    }
    else if (Input.GetKeyUp(KeyCode.A)) 
    {
        x_m = 0;
    }

    if (Input.GetKey(KeyCode.D))
    {
        x_m = x_m + Time.deltaTime * moveSpeed;
        transform.Translate(transform.right * x_m, Space.World);

    }
    else if (Input.GetKeyUp(KeyCode.D)) 
    {
        x_m = 0;
    }


    if (Input.GetKey(KeyCode.Q))
    {
        y_m = y_m - Time.deltaTime * moveSpeed;
        transform.Translate(transform.up * y_m, Space.World);
    }
    else if (Input.GetKeyUp(KeyCode.Q)) 
    {
        y_m = 0;
    }

    if (Input.GetKey(KeyCode.E))
    {
        y_m = y_m + Time.deltaTime * moveSpeed;
        transform.Translate(transform.up * y_m, Space.World);
    }
    else if (Input.GetKeyUp(KeyCode.E))
    {
        y_m = 0;
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值