[英]Unity Jumping Mechanic not working
I have a game in a 3D space, and rigidbody comp. 我在3D空间中有一个游戏,并且有刚体组合。 to a player.
给玩家 I have implemented a jump mechanic that is not working at intended, with this code:
我使用以下代码实现了一个无法正常工作的跳转机制:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public bool Travel;
public Rigidbody rb;
public Transform tf;
public bool Grounded;
public float speed;
int OnRail = 2;
void Update ()
{
float speed = 1f * Time.deltaTime;
if (Input.GetKeyDown("d") && OnRail < 3)
{
tf.Translate(5.4f, 0f, 0f);
OnRail++;
}
else if (Input.GetKeyDown("a") && OnRail > 1)
{
tf.Translate(-5.4f, 0f, 0f);
OnRail--;
}
}
void FixedUpdate ()
{
if (Travel)
{
rb.velocity = new Vector3(0, 0, speed * Time.deltaTime);
}
if (Input.GetKeyDown("space") && Grounded)
{
Grounded = false;
rb.AddForce(new Vector3(0, 500000f * Time.deltaTime, 0));
}
if ((tf.position.y == 2f || tf.position.y == 10f) && rb.velocity.y == 0 && !Grounded)
{
Grounded = true;
}
}
}
The player is supposed to jump into the air, but only jumps almost no distance. 玩家应该跳到空中,但是几乎没有跳距离。 The 2 and 10 Y positions are preset floors (I really don't need these 2 Y-coordinates. unless someone has a substitution for floor detection).
2和10 Y位置是预设的楼层(我真的不需要这2个Y坐标。除非有人可以代替楼层检测)。
This is because GetKeyDown
only occurs for one frame, meaning you are only applying a force upwards for that single frame. 这是因为
GetKeyDown
仅发生在一帧中,这意味着您仅对该帧施加向上的力。 Additionally, Time.deltaTime
is the amount of time it took to complete the last frame (a very small value), meaning you are applying a very small force upward. 另外,
Time.deltaTime
是完成最后一帧所花费的时间(非常小的值),这意味着您要施加很小的向上力。 If you only want a single force applied on spacebar, there is no need to use Time.deltaTime here. 如果只想对空格键施加一个力,则无需在此处使用Time.deltaTime。 Just do:
做就是了:
rb.AddForce(Vector3.up * 100f); // or some other reasonable multipliier
Hi for those that land on this looking for more information on their rigidbodies not behaving the way that they expected, I extracted bit of relevant information from Unity ForceMode's . 嗨,对于那些希望在其刚体上以不符合预期方式的更多信息的人,我从Unity ForceMode的中提取了一些相关信息。 Happy Developing.
愉快的发展。
//Here, switching modes depend on button presses in the Game mode
switch (m_ModeSwitching)
{
//This is the starting mode which resets the GameObject
case ModeSwitching.Start:
//This resets the GameObject and Rigidbody to their starting positions
transform.position = m_StartPos;
m_Rigidbody.transform.position = m_StartForce;
//This resets the velocity of the Rigidbody
m_Rigidbody.velocity = new Vector3(0f, 0f, 0f);
break;
//These are the modes ForceMode can force on a Rigidbody
//This is Acceleration mode
case ModeSwitching.Acceleration:
//The function converts the text fields into floats and updates the Rigidbody’s force
MakeCustomForce();
//Use Acceleration as the force on the Rigidbody
m_Rigidbody.AddForce(m_NewForce, ForceMode.Acceleration);
break;
//This is Force Mode, using a continuous force on the Rigidbody considering its mass
case ModeSwitching.Force:
//Converts the text fields into floats and updates the force applied to the Rigidbody
MakeCustomForce();
//Use Force as the force on GameObject’s Rigidbody
m_Rigidbody.AddForce(m_NewForce, ForceMode.Force);
break;
//This is Impulse Mode, which involves using the Rigidbody’s mass to apply an instant impulse force.
case ModeSwitching.Impulse:
//The function converts the text fields into floats and updates the force applied to the Rigidbody
MakeCustomForce();
//Use Impulse as the force on GameObject
m_Rigidbody.AddForce(m_NewForce, ForceMode.Impulse);
break;
//This is VelocityChange which involves ignoring the mass of the GameObject and impacting it with a sudden speed change in a direction
case ModeSwitching.VelocityChange:
//Converts the text fields into floats and updates the force applied to the Rigidbody
MakeCustomForce();
//Make a Velocity change on the Rigidbody
m_Rigidbody.AddForce(m_NewForce, ForceMode.VelocityChange);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.