[英]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.