gpt4 book ai didi

c# - Unity3D Rigidbody 播放器在运行时跳跃受限

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:47 25 4
gpt4 key购买 nike

我正在尝试使用Rigidbody 对主要 FPS 角色的 Action 进行编程。

相机和 ZQSD 位移效果很好,但移动时跳跃非常受限:

  • 固定跳跃:Y(分钟)= 1; Y(最大)= 2.7;
  • 移动跳跃:Y(分钟)= 1; Y(最大值)= 1.9;

这非常令人沮丧,尤其是对于平台游戏而言。 Personnaly,我希望这两个操作得到相同的结果。

这是我的 C# 代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FPSMovement : MonoBehaviour {

public float walkAcceleration = 150f;
public float maxWalkSpeed = 10f;
public float jumpVelocity = 500f;
public float maxSlope = 45f;

private Rigidbody rb;
private Vector2 horizontalMovement;
private bool isGrounded = false;
private bool doubleJumped = false;

// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
if(rb.velocity.magnitude > maxWalkSpeed){
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxWalkSpeed);
}

transform.rotation = Quaternion.Euler (0, GetComponentInChildren<FPSCamera>().currentYRotation, 0);

// Entrées clavier ZQSD déplaçant le joueur
float x = 0, y = 0, z = 0;
if (Input.GetKey (KeyCode.Z)) {
x += 1f;
}
if (Input.GetKey (KeyCode.S)) {
x -= 1f;
}
if (Input.GetKey (KeyCode.Q)) {
z -= 1f;
}
if (Input.GetKey (KeyCode.D)) {
z += 1f;
}
// Arrêt prompt du glissement
if ((!Input.GetKey (KeyCode.Z) && !Input.GetKey (KeyCode.S) && !Input.GetKey (KeyCode.Q) && !Input.GetKey (KeyCode.D))
&& isGrounded) {
rb.velocity /= 1.1f;
}
// Saut du joueur
if (Input.GetKey (KeyCode.Space) && isGrounded) {
rb.AddForce (0, jumpVelocity, 0);
// Deuxième saut
}
if (Input.GetKey (KeyCode.Space) && !doubleJumped) {
rb.AddForce (0, jumpVelocity, 0);
doubleJumped = true;
}

rb.AddRelativeForce (z * walkAcceleration, 0, x * walkAcceleration);
}

void OnCollisionStay(Collision other) {
foreach (ContactPoint contact in other.contacts) {
if (Vector3.Angle (contact.normal, Vector3.up) < maxSlope) {
isGrounded = true;
doubleJumped = false;
}
}
}

void OnCollisionExit() {
isGrounded = false;
}


}

我也有印象,玩家在空中移动时会悬停,但这不是主要问题,最重要的只是印象。

一个简单的问题,需要用几句话来解释,但对于这类游戏来说却是一个真正需要解决的问题!

我可以请你,Stackoverflow 的人温柔地帮助我,先行伙伴的一千个感谢。

PS:我的母语是法语,所以请不要在语法或其他英语微妙之处浪费太多时间:)

最佳答案

这是因为您限制了玩家的总速度,而您应该只限制水平速度。如果你以最大速度行走并跳跃,你的跳跃速度会加到总速度中,你的角色实际上应该以比最大行走速度更高的速度行进。要修复它,您可以这样做:

// Isolate the horizontal component
Vector3 horizontalVelocity = rb.velocity;
horizontalVelocity.y = 0;

if (horizontalVelocity.magnitude > maxWalkSpeed) {
// Clamp the horizontal component
Vector3 newVelocity = Vector3.ClampMagnitude(horizontalVelocity, maxWalkSpeed);
// Keep the original vertical velocity (jump speed)
newVelocity.y = rb.velocity.y;
rb.velocity = newVelocity;
}

关于c# - Unity3D Rigidbody 播放器在运行时跳跃受限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43933962/

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