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

redis实现分布式锁的原理

Network LSTM both short-term and long-term memory

深度学习之 10 卷积神经网络2

What exactly does a firmware engineer do?

LINGO 18.0软件安装包下载及安装教程

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

慢 SQL 分析与优化

Mysql OCP 28题

Flink Yarn Per Job - 启动AM

015-Balanced binary tree (1)
随机推荐
阿里云·短信发送
【LeetCode】226. Flip the binary tree
【LeetCode】101.对称二叉树
Let‘s Encrypt 使用
基于二次型性能指标的燃料电池过氧比RBF-PID控制
Mysql OCP 27题
mysql8安装步骤教程
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_关于JSON数据的查询
多媒体数据处理实验2:PCA
兔起鹘落全端涵盖,Go lang1.18入门精炼教程,由白丁入鸿儒,全平台(Sublime 4)Go lang开发环境搭建EP00
LINGO 18.0软件安装包下载及安装教程
【LeetCode】226.翻转二叉树
Mysql OCP 29题
Scrapy + Selenium 实现模拟登录,获取页面动态加载数据
Chrome F12 keep before request information network
Flink Yarn Per Job - 提交应用
Go的Gin框架学习
Machine learning (formula derivation and code implementation)--sklearn machine learning library
SQL试题