unity通过鼠标控制物体的360度旋转;实现物体在场景的自由穿梭

323 阅读2分钟

1.实现项目运行鼠标隐藏,Esc键显示鼠标

    void Start()
    {

        Cursor.lockState = CursorLockMode.Locked; //设置鼠标在游戏中隐藏  体
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.None;
        }
        //重新点击后再次进入鼠标开启
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Cursor.lockState = CursorLockMode.Locked;
        }
    }

2.通过鼠标控制物体的360度旋转(原地)

1.将脚本文件camera挂载到摄像头

image.png

2.编写camera脚本文件

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

public class camera : MonoBehaviour
{
    //鼠标移动的灵敏度
    public float speed = 6f;
    //鼠标的x,y的偏移量
    private float x;
    private float y;
    private Transform players;
    private float xRotation; //用来记录每次更改后的x的偏移量,应该是在每次基础上进行改变,而不是重置
    // Start is called before the first frame update
    void Start()
    {

        Cursor.lockState = CursorLockMode.Locked; //设置鼠标在游戏中隐藏  
        players = transform.parent; //拿到当前物体的第一个孩子--相机 gameObject--当前的胶囊体
    }

    // Update is called once per frame
    void Update()
    {
        //鼠标x y的偏移量
        x = Input.GetAxis("Mouse X") * speed;
        y = Input.GetAxis("Mouse Y") * speed;
        xRotation += x;
        Debug.Log($"{x},{y}");
        //让物体绕y轴旋转移动偏移量
        Vector3 dir = new Vector3(-y, 0, 0);
        //改变相机的转向
        transform.Rotate(dir); //相对于原来的位置进行旋转
        //改变players的转向

        players.rotation = Quaternion.Euler(0, xRotation, 0); //改变他的方向 度数一直记录着

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.None;
        }
    
}
}

3.效果如下:

动画.gif

2.实现物体在场景的自由穿梭

1.给胶囊体物体挂载players脚本文件

image.png

2.给胶囊体物体添加角色控制器和刚体控制器

image.png 3.编写players脚本文件内容

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

public class players : MonoBehaviour
{
    private float speed = 4f;
    private float x;
    private float z;
    private CharacterController a;
    private Vector3 dir; //创建一个向量
    void Start()
    {
        //拿到players身上的角色控制器
        a = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        x = Input.GetAxis("Horizontal") * speed;
        z = Input.GetAxis("Vertical") * speed;
        //  z轴是前进的方向-forward 获取移动的方向向量
        dir = transform.right * x + transform.forward * z;
        //使用SimpleMove有重力效果--可以解决move方法引起的悬浮太高的问题。
        a.SimpleMove(dir*speed);
    }
}

4.效果如下:

动画.gif

1.rigidbody刚体介绍:

通过物理模拟控制对象的位置。

向对象添加 Rigidbody 组件后,其运动将受到 Unity 物理引擎的控制。即使不添加任何代码,Rigidbody 对象也受到向下的重力,并在与其他对象碰撞时作出反应

注意:

在脚本中,建议使用 [FixedUpdate]函数来施加力和更改 Rigidbody 设置(而不是使用 [Update],Update 用于大多数其他帧更新任务)。这样做的原因是物理更新在测量的时间步骤中执行,而时间步骤与帧更新不一致。FixedUpdate 在每次进行物理更新前调用,因此在该函数中做出的任何更改都将直接处理

2.character Controller角色控制器介绍 为 GameObject 的移动提供附加的 CharacterController 组件。

[CharacterController.Move] 运动在给定方向移动 GameObject。给定方向需要绝对移动增量值。碰撞约束 [Move] 的发生。返回值 [CollisionFlags] 指示碰撞的方向:None、Sides、Above 和 Below。[CharacterController.Move]不使用重力。