当前位置:网站首页>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);
}
}
}
边栏推荐
猜你喜欢
随机推荐
C语言二维数组用一维数组进行调用
Redis集群概念与搭建
Flink Yarn Per Job - 创建启动Dispatcher RM JobManager
ORA-06512 数字或值错误字符串缓冲区太小
array of function pointers
MySQL 免安装版的下载与配置教程
浅聊缓存函数
ClickHouse 数据插入、更新与删除操作 SQL
10 Convolutional Neural Networks for Deep Learning 2
Mysql OCP 27题
LINGO 18.0 software installation package download and installation tutorial
英文语法-状语从句
Flink Yarn Per Job - 启动AM
php中去重二维数组
二叉查找树的插入
The window of the chosen data flow
013-Binary tree
【LeetCode】101.对称二叉树
基于二次型性能指标的燃料电池过氧比RBF-PID控制
SAP Analytics Cloud 和 SAP Cloud for Customer 两款 SaaS 软件的集成