Unity 与地面碰撞不起作用(菜鸟)
因此,我刚刚开始与Unity合作,并希望我的球员只有在接触地面时才能跳跃。如果玩家用“地面”标签触摸游戏对象,我给了我的地面“地面”标签,然后检查了地面标签,以便将接地的布尔设置为真。不知何故,我的碰撞行不通。
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float speed = 10;
private Rigidbody2D body;
private Animator animate;
private bool grounded;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
animate = GetComponent<Animator>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (horizontalInput > 0.01f)
transform.localScale = new Vector3(7, 7, 7);
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-7, 7, 7);
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
Jump();
animate.SetBool("Run", horizontalInput != 0);
animate.SetBool("grounded", grounded);
}
private void Jump()
{
body.velocity = new Vector2(body.velocity.x, speed);
animate.SetTrigger("Jump");
grounded = false;
}
private void OncollisionEnter2D(Collision2D collision)
{
Debug.Log("Collision detected");
if (collision.gameObject.tag == "Ground")
grounded = true;
Debug.Log("ground is being touched");
}
}
我认为让此过程的某些步骤输出日志消息是一个好主意,但是我的日志在测试后保持空。
So I just started working with unity and wanted that my player can only jump when he's touching the ground. I gave a "Ground" tag to my ground and checked, If the player is touching the gameObject with the "Ground" tag, so the grounded bool would be set to true. Somehow my collision doesnt work.
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float speed = 10;
private Rigidbody2D body;
private Animator animate;
private bool grounded;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
animate = GetComponent<Animator>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (horizontalInput > 0.01f)
transform.localScale = new Vector3(7, 7, 7);
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-7, 7, 7);
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
Jump();
animate.SetBool("Run", horizontalInput != 0);
animate.SetBool("grounded", grounded);
}
private void Jump()
{
body.velocity = new Vector2(body.velocity.x, speed);
animate.SetTrigger("Jump");
grounded = false;
}
private void OncollisionEnter2D(Collision2D collision)
{
Debug.Log("Collision detected");
if (collision.gameObject.tag == "Ground")
grounded = true;
Debug.Log("ground is being touched");
}
}
I thought it would be a good idea to let certain steps of this process output a log message, but my log stayed empty after testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OnCollisionEnter2D
是您的正确拼写有意写。因此,将OncollisionEnter2D
更改为OnCollisionEnter2D
,因为大小写很重要。由于我们已经在这里并且不会造成伤害,我还建议您使用
CompareTag
而不是tag == string
因为它的性能稍好一些,因为它更少内存分配(您可以在此处阅读:https://learn.unity.com/tutorial/fixing-performance-problems#5c7f8528edbc2a002053b595),因此习惯CompareTag
是一个好习惯:要完成此操作答案,我想指出,您可能需要考虑修复自动完成建议(在 Visual Studio 和 Visual Studio Code: IntelliSense 中)如果它不起作用。借助此工具,您将永远不会再拼写错误,因为一旦您开始写作,就会建议正确的名称。
OnCollisionEnter2D
would be the right spelling of what you intented to write. So, changeOncollisionEnter2D
toOnCollisionEnter2D
, because capitalization matters.As we're already here and it won't harm, I'd also suggest you to use
CompareTag
instead oftag == string
as it performs slightly better due to less memory allocation (as you can read here: https://learn.unity.com/tutorial/fixing-performance-problems#5c7f8528edbc2a002053b595), so it's a good habit to get used toCompareTag
:To finish up this answer, I'd like to point out that you might want to consider fixing your auto-completion suggestions (in Visual Studio and Visual Studio Code: IntelliSense) in case it doesn't work. With the help of this tool, you'll never misspell something like this again, because the correct name will be suggested as soon as you start writing.
与你想要的不同,这个解决方案缺乏响应能力,你不能重复跳跃。 (https://www.youtube.com/watch?v=LEUhxe9vUOM )这个视频家伙使用 Boxcast。通过这种方式,boxcast方法在碰撞盒周围创建一个盒子,但它的一个参数是y偏移,在碰撞盒接触地面之前,这个盒子接触地面,这样你就可以重复跳跃。
Different from what you wanted, this solution lacks responsiveness, you cant jump repeatedly. (https://www.youtube.com/watch?v=LEUhxe9vUOM )in this video dude uses Boxcast. With this way, boxcast method creates box around the collision box, but one argument of it y offset, before collision box touches the ground, this box touches the ground thus you can jump repeadetly.