[英]Unity 2D Character Don't Stop Jumping
Okay so I changed the code and added new c# script.好的,所以我更改了代码并添加了新的 c# 脚本。 i checked the box coliders they are good.我检查了 box colider 他们很好。 Now the problem is that I cant jump.现在的问题是我不能跳。
This is what I changed in Move2DPlayer这是我在 Move2DPlayer 中改变的
private void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
This is the new c# its called "Grounded"这是新的 c#,它被称为“Grounded”
public class Grounded : MonoBehaviour
{
GameObject Player;
private void Start()
{
Player = gameObject.transform.parent.gameObject;
}
private void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<Move2DPlayer>().isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
Player.GetComponent<Move2DPlayer>().isGrounded = false;
}
}
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(rb.position, checkRadius, whatIsGround);
//Rest of your stuff
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
Okay, so I managed to get a test project working with this. 好的,所以我设法得到了一个与此相关的测试项目。 If this doesn't work for you some the only thing I can think of is that your Rigidbody might too large making the collision detection from OverlapCircle not work correctly. 如果这对您不起作用,那么我唯一想到的就是您的刚体可能太大,导致OverlapCircle的碰撞检测无法正常工作。 Make sure to set checkRadius large enough to actually detect the ground. 确保将checkRadius设置得足够大以实际检测地面。
Your player probably has a Collider2D
itself and either whatIsGround
is not configured correctly or your player has a wrong layer so 您的播放Collider2D
本身可能具有Collider2D
,或者whatIsGround
配置不正确,或者播放器具有错误的图层,因此
Physics2D.OverlapCircle(rb.position, checkRadius, whatIsGround);
returns always true
because you collide with the player itself. 由于您与播放器本身发生碰撞,因此始终返回true
。
I tested your code without any changes and it works for me: 我测试了您的代码,没有任何更改,它对我有用:
However some performance and other considerations: 但是,还有一些性能和其他注意事项:
Do not Debug.Log
in Update
or FixedUpdate
.. for development it's fine but make sure to remove it later! 请勿Debug.Log
Update
或FixedUpdate
..以便进行开发,但请确保稍后将其删除! It's very performance intense. 这是非常强烈的表现。
You can set the Inspector to debug mode. 您可以将检查器设置为调试模式。
This way you can see the values of private fields and you should notice that isGrounded
is probably always true
in your case 这样,您可以查看私有字段的值,并且应注意,在您的情况下, isGrounded
可能始终为true
Just a beautify thing but instead of somebool == true
and somebool == false
rather simply use somebool
and !somebool
只是美化事物,而不是使用somebool == true
和somebool == false
而是简单地使用somebool
和!somebool
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.