【问题标题】:Unity OnCollisionStay2D giving error: "PlayerMovement.cs(45,44): error CS1001: Identifier expected"Unity OnCollisionStay2D 给出错误:“PlayerMovement.cs(45,44): error CS1001: Identifier expected”
【发布时间】:2019-07-30 10:14:51
【问题描述】:

我正在 Unity 中制作一个简单的 2D 游戏,并且我正在努力让我的角色只有在触地时才能跳跃。这是我的代码:

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

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float movementSpeed = 5.0f;
    [SerializeField] private bool isGrounded = false;

    private Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        move();
        jump();
    }

    private void move()
    {
        float move = Input.GetAxis("Horizontal");
        rb.velocity = new Vector2(movementSpeed * move * Time.deltaTime, rb.velocity.y);
    }

    private void jump()
    {
        if(Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
        {
            Debug.Log("jump");
        }
    }

    void OnCollisionStay2D(Collision2D, col)
    {
        if(col.gameObject.tag == "Ground")
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D, col)
    {
        if(col.gameObject.tag == "Ground")
        {
            isGrounded = false;
        }
    }
}

当我运行它时,它给了我这个错误:“PlayerMovement.cs(45,44): error CS1001: Identifier expected”

我尝试在 OnCollisionStay 中使用 Collider2D 而不是 Collision2D,但这也不起作用。提前感谢您的帮助!

【问题讨论】:

  • 你觉得这个方法原型是什么意思? void OnCollisionStay2D(Collision2D, col)。你真的有两个独立的参数吗?

标签: c# unity3d


【解决方案1】:

(Collision2D, col)改成(Collision2D col)

void OnCollisionStay2D(Collision2D col)
        {
            if(col.gameObject.tag == "Ground")
            {
                isGrounded = true;
            }
        }

        void OnCollisionExit2D(Collision2D col)
        {
            if(col.gameObject.tag == "Ground")
            {
                isGrounded = false;
            }
        }

【讨论】:

  • 谢谢蒂姆!效果很好,我想我只是不够专注,没有意识到我有一个逗号。
猜你喜欢
  • 2021-07-12
  • 2021-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2023-02-03
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
相关资源
最近更新 更多