当前位置:网站首页>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);
}
}
}
边栏推荐
猜你喜欢

【LeetCode】112.路径总和

Redis集群概念与搭建

pytorch安装错误

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现

Redis实现分布式锁

机器学习(公式推导与代码实现)--sklearn机器学习库

Go操作Redis数据库

Rabbit and Falcon are all covered, Go lang1.18 introductory and refined tutorial, from Bai Ding to Hongru, the whole platform (Sublime 4) Go lang development environment to build EP00

MySQL的分页你还在使劲的limit?

【LeetCode】zj面试-把字符串转换成整数
随机推荐
文章列表的显示 以及创建文章 还有文章详情的基本
110 MySQL interview questions and answers (continuous updates)
Machine learning (formula derivation and code implementation)--sklearn machine learning library
10 minutes to get you started chrome (Google) browser plug-in development
Redis集群概念与搭建
子查询和关联查询的区别
命令行加载特效 【cli-spinner.js】 实用教程
二叉查找树的插入
固件工程师到底是干什么?
System io statistics
使用 Scrapy 框架对重复的 url 无法获取数据,dont_filter=True
MySQL——几种常见的嵌套查询
验证浮点数输入
Chrome F12 keep before request information network
慢 SQL 分析与优化
oracle中的rownum函数
英文语法-状语从句
gpnmb+ gpnmb-AT2 cell空转映射 上皮细胞的空转映射
mysql的union和union all
WinCheck Script