试图编写一个2D Unity代码,以显示我的角色是否已接地并且不起作用

问题描述 投票:-1回答:2
error CS1061: 'Collision2D' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'Collision2D' could be found

我如何打磨地面,以便角色识别出它是地面?我正在尝试进行2D跳跃运动。是Collisions2D找不到GetComponent,还是游戏可以运行,但角色根本没有跳跃。

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

public class Grounded : MonoBehaviour
{
    GameObject Player;

    void Start()
    {
        Player = GetComponentInParent<GameObject>();
    }

    void Update(){
    }

    void OnCollisionEnter2D(Collision2D col) {

        if (col.GetComponent<Collider2D>().tag == "Ground") {

            Player.GetComponent<Move2D>().isGrounded = true;

        }
    }

    void OnCollisionExit2D(Collision2D col) {

        if (col.GetComponent<Collider2D>().tag == "Ground") {

            Player.GetComponent<Move2D>().isGrounded = false;

        }
    }
}
c# unity3d 2d collision
2个回答
0
投票

要使用Collider2D组件,请使用gameObject属性。替换此行:


0
投票

尝试改用col.collider.tag == "Ground"

© www.soinside.com 2019 - 2024. All rights reserved.