gpt4 book ai didi

c# - 在 Unity3D 中使用第一人称 Controller 在空中移动,c#

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:32 24 4
gpt4 key购买 nike

我目前正在尝试在我试图在我的项目中实现的这个第一人称角色 Controller 中启用空中移动。目前,玩家在空中时会失去所有移动控制。

我试图在跳跃机制中复制正常的移动控制,但无济于事。对于问题的简单性,我深表歉意,在编码方面我是一个新手。

非常感谢所有阅读本文的人:)

   [SerializeField] private string verticalInputName;
[SerializeField] private float movementSpeed;

private CharacterController charController;

[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;


private bool isJumping;

private void Awake()
{
charController = GetComponent<CharacterController>();
}

private void Update()
{
PlayerMovement();
}

private void PlayerMovement()
{
float horizInput = Input.GetAxis(horizontalInputName) * movementSpeed;
float vertInput = Input.GetAxis(verticalInputName) * movementSpeed;

Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horizInput;

charController.SimpleMove(forwardMovement + rightMovement);

JumpInput();

}

private void JumpInput()
{
if (Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
StartCoroutine(JumpEvent());
}
}

private IEnumerator JumpEvent()
{
charController.slopeLimit = 90.0f;
float timeInAir = 0.0f;


do
{

float jumpForce = jumpFallOff.Evaluate(timeInAir);
charController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);

timeInAir += Time.deltaTime;

yield return null;
} while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);

float horizInput = Input.GetAxis(horizontalInputName) * movementSpeed;
float vertInput = Input.GetAxis(verticalInputName) * movementSpeed;

Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horizInput;

charController.SimpleMove(forwardMovement + rightMovement);
isJumping = false;
}
}

最佳答案

文档 recommends每帧只调用 MoveSimpleMove 一次,但在协程和 PlayerMovement

您应该将跳转逻辑移到协程之外,这样您就可以在每次调用之前执行跳转例程逻辑,然后然后Move/ 进行一次调用SimpleMove,包括来自跳跃逻辑和玩家输入的计算。

这里有一种方法可以将 Move/SimpleMove 的使用减少到每帧一次调用:

[SerializeField] private string horizontalInputName;
[SerializeField] private string verticalInputName;
[SerializeField] private float movementSpeed;

private CharacterController charController;

[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;


private bool isJumping = false;
private float timeInAir = 0.0f;

private void Awake()
{
charController = GetComponent<CharacterController>();
}

private void Update()
{
PlayerMovement();
}

private void PlayerMovement()
{
float forwardInput = Input.GetAxis(verticalInputName);
float rightInput = Input.GetAxis(horizontalInputName);

Vector3 forwardMovement = transform.forward * forwardInput;
Vector3 rightMovement = transform.right * rightInput;
Vector3 horizMovement = (forwardMovement+rightMovement) * movementSpeed;

float jumpVelocity = JumpInput();

if (isJumping)
{
Vector3 vertMovement = Vector3.up * jumpVelocity * jumpMultiplier;
charController.Move((horizMovement + vertMovement) * Time.deltaTime)
}
else
{
charController.SimpleMove(horizMovement);
}
}

private float JumpInput()
{
float jumpVelocity = 0f;

if (Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
charController.slopeLimit = 90.0f;
timeInAir = 0f;
}

if (isJumping){
if (timeInAir == 0f ||
(!charController.isGrounded
&& charController.collisionFlags != CollisionFlags.Above))
{
jumpVelocity = jumpFallOff.Evaluate(timeInAir);
timeInAir += Time.deltaTime;
}
else
{
ifJumping = false;
}
}

return jumpVelocity;
}

关于c# - 在 Unity3D 中使用第一人称 Controller 在空中移动,c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57117998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com