unity3d - FixedUpdate 不注册所有键(统一)
问题描述
我订购了一本书,我一直完全按照它的说明进行操作。唯一的问题是,当我将动作放入 FixedUpdate 时,它不会注册每个键。我已经阅读了很多答案,但他们都说将您的输入放入 Update 并将所有物理放入 FixedUpdate。我就是这么做的,但是当我按空格键 30 次时,它只会跳到 5 左右。这是我的一些代码(如果它的可读性不好,我很抱歉,我才刚刚开始。):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehavior : MonoBehaviour
{
public GameObject bullet;
public float moveSpeed = 10f;
public float rotateSpeed = 10f;
public float jumpVelocity = 5f;
public float distanceToGround = 0.1f;
public float bulletSpeed = 100f;
public LayerMask groundLayer;
private float vInput;
private float hInput;
private Rigidbody _rb;
private CapsuleCollider _col;
private bool isShooting;
private bool isJumping;
void Start()
{
_rb = GetComponent<Rigidbody>();
_col = GetComponent<CapsuleCollider>();
isShooting = false;
isJumping = false;
}
void Update()
{
//player movement
vInput = Input.GetAxis("Vertical") * moveSpeed;
hInput = Input.GetAxis("Horizontal") * rotateSpeed;
isJumping = Input.GetKeyDown(KeyCode.Space);
//allows me to see if the spacer bar is registering
if (isJumping)
{
Debug.Log("Jump");
}
isShooting = Input.GetMouseButtonDown(1);
if (isShooting)
{
Debug.Log("Shoot");
}
}
void FixedUpdate()
{
Vector3 rotation = Vector3.up * hInput;
Quaternion angleRot = Quaternion.Euler(rotation * Time.fixedDeltaTime);
_rb.MovePosition(this.transform.position + this.transform.forward * vInput *
Time.fixedDeltaTime);
_rb.MoveRotation(_rb.rotation * angleRot);
//determines if the player is on the ground and hit the spacebar. If so the player jumps.
if (IsGrounded() && isJumping)
{
_rb.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);
//this allows isJumping to revert back to false 100% of the time
isJumping = false;
Debug.Log("The jump mechanic is working");
}
if (isShooting)
{
GameObject newBullet = Instantiate(bullet, this.transform.position + new Vector3(1, 0, 0),
this.transform.rotation) as GameObject;
Rigidbody bulletRB = newBullet.GetComponent<Rigidbody>();
bulletRB.velocity = this.transform.forward * bulletSpeed;
isShooting = false;
Debug.Log("The shooting mechanic is working");
}
}
//determines if the player is on the ground
bool IsGrounded()
{
Vector3 capsuleBottom = new Vector3(_col.bounds.center.x, _col.bounds.min.y,
_col.bounds.center.z);
bool grounded = Physics.CheckCapsule(_col.bounds.center, capsuleBottom, distanceToGround,
groundLayer, QueryTriggerInteraction.Ignore);
return grounded;
}
}
解决方案
FixedUpdate
每 200 毫秒左右调用一次,具体取决于您将 FixedUpdate 间隔设置为什么。
Update
每帧调用一次。
GetKeyDown
在您按目标键true
的确切帧上返回KeyCode
。在它返回的每隔一帧false
。
如果您调用GetKeyDown
中的每一帧Update
,isJumping
将是您true
按下键的确切帧,以及每隔一帧false
。
现在想象一下这会如何影响不是每帧都调用FixedUpdate
的方法。可以设置为和before甚至被调用一次。IsJumping
true
false
FixedUpdate
您基本上只想设置IsJumping
为true
inUpdate
然后将其设置为false
in FixedUpdate
。
您的 Update 方法最终应如下所示:
void Update()
{
//player movement
vInput = Input.GetAxis("Vertical") * moveSpeed;
hInput = Input.GetAxis("Horizontal") * rotateSpeed;
if(Input.GetKeyDown(KeyCode.Space)){
isJumping = true;
}
if(Input.GetMouseButtonDown(1)){
isShooting= true;
}
}
推荐阅读
- javascript - 将人重定向到另一个页面后执行点击功能
- javascript - 从同一路线获得过滤后的产品。节点,猫鼬,MongoDB
- authentication - 微服务中的身份验证:每个用户一个会话
- javascript - AppendChild 除了另一个元素
- python - 如何清除/删除频道中特定用户的消息
- java - 列表有值但没有显示在浏览器或邮递员中,它显示为空白条目
- html - CSS 过渡不透明度适用于 0 到 1,但不适用于 1 到 0
- java - 代码在我输入后按回车之前执行
- python - python - 如何在不停止主线程的情况下运行后台线程
- sql - 数据库 | Postgres | 如何检查 IP 是否在 IP 列表或范围中