当前位置:网站首页>Unity realizes first-person roaming (nanny-level tutorial)
Unity realizes first-person roaming (nanny-level tutorial)
2022-08-05 06:15:00 【gentle brother】
前言
This article is to explain how to use already written code to passunityImplement the function of first-person roaming,就是说你可以Just use the code below,如果你想深入学习,You can refer to the more detailed comments in the code and consult the relevant documentation.
You can see the finished effect first
漫游
步骤
1.创建 CameraController 和 PlayerController 两个C# Script
* CameraController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
//我们通过控制Player的旋转方法,to control the left and right movement of the camera's perspective,所以我们需要一个Player的Tranform
public Transform player;
//定义两个float变量,to get the value of the mouse movement
private float mouseX, mouseY;
//We can add a sensitivity to the mouse
public float mouseSensitivity;
//mouseY中的GetAxis方法会返回-1到1之间的浮点数,在鼠标移动的时候,数值会随着方向的变化而变化,在鼠标不动时,数值会回弹到0,So we will encounter the problem of rebound when the mouse is moved up and down
public float xRotation;
private void Update()
{
//在Update方法中,We use the input from the systemGetAxis方法to get the value of the mouse movement,Multiply by the mouse sensitivity and then multiplyTime.deltatime,The value of the mouse movement is thus obtained
//Input.GetAxis:It will return during mouse movement of the corresponding axis -1 到 1 的值
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
//使用数学函数Clamp限制
xRotation = Mathf.Clamp(xRotation,-70f,70f);
//这里使用Transform的Rotate()方法来旋转player
//Vector3.upis a three-dimensional variable up,和一个0,1,0The three-dimensional variables are the same
//我们需要控制player的yThe axis rotates to make it rotate left and right
player.Rotate(Vector3.up * mouseX);
//Next we have to choose the camera,我们使用tranform.localRotation方法,让相机上下旋转,使用localRotationIt is not affected by the rotation of the parent object,causing some weird problems
//因为localRotation是属性,We also have to assign him a value
transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
}
}
*PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//获得player的CharacterController组件
private CharacterController cc;
public float moveSpeed;//移动速度
public float jumpSpeed;//跳跃速度
//定义获得按键值的两个变量
private float horizontalMove, verticalMove;
//定义三维变量dir控制方向
private Vector3 dir;
//重力
public float gravity;
private Vector3 velocity;//用来控制Y轴速度
//我们只需要检测player是否在地上就可以了,这里我们可以使用Physics中的CheckSphere方法,如果定义的球体和物体发生碰撞,返回真
//in order to use this method,我们需要定义几个变量
public Transform groundCheck;//检测点的中心位置
public float checkRedius;//检测点的半径
public LayerMask groundLayer;//The layer that needs to be detected
//布尔值来存储CheckSphere的返回值
public bool isGround;
private void Start()
{
//获取player的CharacterController组件
cc = GetComponent<CharacterController>();
}
private void Update()
{
isGround = Physics.CheckSphere(groundCheck.position,checkRedius,groundLayer);
if(isGround && velocity.y < 0)
{
velocity.y = -2f;
}
horizontalMove = Input.GetAxis("Horizontal") * moveSpeed;
verticalMove = Input.GetAxis("Vertical") * moveSpeed;
dir = transform.forward * verticalMove + transform.right * horizontalMove;
cc.Move(dir * Time.deltaTime);
//我们需要获取到跳跃按键的事件,使用Input中的GetButtonDown()方法,他会返回一个布尔值,Returns true when pressed
//Jump可以在InputManager中查看
//There is an upward velocity in an instant,在过程中也会随着重力慢慢下降,If you want it to jump only once,加上isGround就行了
if(Input.GetButtonDown("Jump") && isGround)
{
velocity.y = jumpSpeed;
}
velocity.y -= gravity * Time.deltaTime;//这样每秒它就会减去重力的值不断下降
//再用CharacterController的Move方法来移动y轴
cc.Move(velocity * Time.deltaTime);
}
}
2.创建一个Environment空物体,Bring environment-related resources to Environment下,点击Environment,将LayerInstead, we created it ourselvesGround层
Because I have already created it in advanceGround层,You need to clickAdd Layer,在最下面创建Ground层就可以了
3.新建一个Capsule,命名为Player
4.Move the main camera to the one you just createdPlayer下,Move the position of the main camera to Player的上半部分
5.Hook up the main cameraCameraController脚本,设置参数:
*Player:就是刚刚创建的Player
*Mouse Sensitivity:200
*xRotation:0
6.创建空物体 GroundCheck,将其放到 Player下,Move the location toPlayer的底部
7.将Player的 Capsule Colider 移除掉,添加 Character Controller 组件
8.将 PlayerController 脚本挂到 Player 上,设置参数:
*Move Speed:4
*Jump Speed:5
*Gravity:9.81
*Ground Check:就是我们刚创建的Ground Check
*Check Redius:0.4
*Ground Layer:We created it in advance Ground 层
结语
希望这篇文章可以帮助到你,The material I have here is from unity 中的 Asset Store 上下载的,If you need this material, you can search for it Low-Poly Simply Nature Pack,有什么问题可以在评论区留言,感谢大家的支持!
边栏推荐
猜你喜欢
随机推荐
技术分享杂七杂八技术
单臂路由与三成交换机
【Day1】VMware软件安装
调用TensorFlow Objection Detection API进行目标检测并将检测结果保存至本地
VRRP原理及命令
ROS video tutorial
unity实现第一人称漫游(保姆级教程)
运维工程师,快来薅羊毛
TCP/IP four-layer model
OpenCV3.0 兼容VS2010与VS2013的问题
I217-V network disconnection problem in large traffic under openwrt soft routing
【Day8】Knowledge about disk and disk partition
图片压缩失效问题
Technology Sharing Miscellaneous Technologies
LeetCode面试题
LeetCode Interview Questions
spark算子-wholeTextFiles算子
入门文档08 条件插件
Spark源码-任务提交流程之-6-sparkContext初始化
TCP/IP四层模型