当前位置:网站首页>Unity笔记之简陋的第一人称漫游
Unity笔记之简陋的第一人称漫游
2022-08-03 09:33:00 【念丶小宇】
每次要用的时候都要写一会,真麻烦。直接复制粘贴吧!
using System;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMove : MonoBehaviour
{
private CharacterController _controller;
[SerializeField] private float moveSpeed = 6.0f;
private Camera mainCamera;
[SerializeField] private float rotateSpeed = 50;
private float x, y;
//重力
[SerializeField] private float gravity = 110f;
[SerializeField] private float jumpSpeed = 8.0f;
private Vector3 moveDirection = Vector3.zero;
private void Awake()
{
_controller = GetComponent<CharacterController>();
mainCamera = Camera.main;
}
private void Update()
{
// Move
if (_controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
_controller.Move(moveDirection * Time.deltaTime);
// Rotate
if (Input.GetMouseButton(1))
{
y = Input.GetAxis("Mouse X");
x = Input.GetAxis("Mouse Y");
mainCamera.transform.eulerAngles +=
new Vector3(-x * Time.deltaTime * rotateSpeed, y * Time.deltaTime * rotateSpeed, 0);
}
}
}
边栏推荐
猜你喜欢

What are pseudo-classes and pseudo-elements?The difference between pseudo-classes and pseudo-elements

MySQL_关于JSON数据的查询

SQL试题

Scala parallel collections, parallel concurrency, thread safety issues, ThreadLocal

Exception: Dataset not found.解决办法

Redis和Mysql数据同步的两种方案

超详细的Asp.net使用SSL双向认证,一篇就够了

浅析什么是伪类和伪元素?伪类和伪元素的区别解析

MySQL8重置root账户密码图文教程

ClickHouse删除数据之delete问题详解
随机推荐
Mysql OCP 27题
Exception: Dataset not found. Solution
oracle ASM磁盘空间的查看
ORA-06512 数字或值错误字符串缓冲区太小
Redis的基础与django使用redis
When deleting a folder, the error "Error ox80070091: The directory is not empty" is reported. How to solve it?
bihash总结
C语言二维数组用一维数组进行调用
cnpm安装步骤
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
oracle中的rownum函数
Chrome F12 keep before request information network
【LeetCode】老虎证券面试-括号嵌套且满足优先级
scala reduce、reduceLeft 、reduceRight 、fold、foldLeft 、foldRight
MySQL8重置root账户密码图文教程
PostgreSQL的架构
MySQL_关于JSON数据的查询
milvus
播放量暴涨2000w+,单日狂揽24w粉,内卷的搞笑赛道还有机会
分区分表(一)