我正在 Unity3d 中开发一个无尽的运行游戏,我遇到了这个问题。我让我的角色跳跃,为此,我使用Unity中的曲线功能来改变跳跃时角色的高度和中心。
然后我就遇到了这个问题。当我按下跳跃按钮时,我使动画剪辑以没有向上推力或任何物理的方式运行。简而言之,我正在降低对撞机高度和中心点。但这样做时,由于我实现了重力,我的角色往往会下降,因为最终我的角色应该会下降。
我唯一不想涉及重力的时候是当我跳跃时(当跳跃动画运行时)。我该怎么做呢。或者对如何解决这个错误有什么建议?
下面是我实现的跳跃代码。
private float verticalVelocity;
public float gravity = 150.0f;
private bool grounded = true;
private bool jump = false;
private float currentY;
private Animator anim;
private AnimatorStateInfo currentBaseState;
private static int fallState = Animator.StringToHash("Base Layer.Fall");
private static int rollState = Animator.StringToHash("Base Layer.Roll");
private static int locoState = Animator.StringToHash("Base Layer.Run");
private static int jumpState = Animator.StringToHash("Base Layer.Jump");
private void Update()
{
currentY = verticalVelocity * Time.fixedDeltaTime;
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
grounded = true;
if (isGround() && currentY < 0f)
{
verticalVelocity = 0f;
currentY = 0f;
grounded = true;
jump = false;
fall = false;
if (currentBaseState.fullPathHash == locoState)
{
if (Input.GetButtonDown("Jump") && grounded && currentY == 0f)
{
grounded = false;
jump = true;
verticalVelocity = 0f; //I have tried here to stop gravity but don't work
follower.motion.offset = new Vector2(follower.motion.offset.x, verticalVelocity);
}
}
else if (currentBaseState.fullPathHash == jumpState)
{
Debug.Log("Jumping state");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
else if (jump)
{
follower.motion.offset = new Vector2(follower.motion.offset.x, 0.0f); //I have tried here to stop gravity but don't work
}
else
{
grounded = false;
jump = false;
fall = true;
}
anim.SetBool("Grounded", grounded);
anim.SetBool("Jump", jump);
anim.SetBool("Fall", fall);
if (fall)
{
if (currentBaseState.fullPathHash == fallState)
{
Debug.Log("falling");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
else
{
if (currentBaseState.fullPathHash == rollState)
{
Debug.Log("Roll");
collider.height = anim.GetFloat("ColliderHeight");
collider.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
MoveLeftRight();
verticalVelocity -= gravity * Time.fixedDeltaTime;
follower.motion.offset = new Vector2(follower.motion.offset.x, currentY);
Z - Forward and Backward
follower.followSpeed = speed;
}
最佳答案
要以编程方式禁用场景的重力,您可以使用:
if (isGrounded)
{
Physics.gravity = new Vector3(0, -9.8f, 0);
} else {
// Here the value you prefer
Physics.gravity = new Vector3(0, -0.1f, 0);
}
但是通过这种方法,场景中的其他元素也不会受到重力的影响,我不确定你是否想要这个。
因此,要禁用特定游戏对象的重力,您可以编写:
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
另一个选择(我自己从未尝试过)是这样的:在游戏对象上施加一个力来补偿重力(y 轴应该是 -9.8f)
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
rb.AddForce(transform.down * 9.8f);
}
或者也许用恒力:
GetComponent<ConstantForce>().force = new Vector3(0, 9.8f, 0);
关于c# - Unity中角色跳跃时如何禁用重力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46122481/