[英]Trying to program a 2D Unity code that shows if my character is grounded or not and it's not working
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;
}
}
}
尝试使用col.collider.tag == "Ground"
代替。
col.collider
指的是col.collider
的对撞机,在您的情况下为地面。 因为那是您要碰撞的对撞机(当然只有当您接触地面时)。
col.otherCollider
可用于调用其他碰撞器,在您的情况下是播放器本身。 这在有很多碰撞时很有用。
如果这还不够,请随时寻求更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.