Collider2D.bounds 是检查地面脚本中非静态字段所必需的
Collider2D.bounds is required for non static field on check ground script
我正在创建检查地面脚本,但在 Collider2D.bounds 上显示错误 非静态字段、方法或 属性 需要对象引用 'Collider2D.bounds'
有剧本:
public class PlayerScript : MonoBehaviour
{
[SerializeField] float speed = 10f;
private Animation rotate_anim;
// Start is called before the first frame update
void Start()
{
rotate_anim = GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
Jump();
}
void Jump(){
//not there
}
private bool isGrounded(){
RaycastHit2D raycasthit2d = Physics2D.BoxCast(CircleCollider2D.bounds.center, CircleCollider2D.bounds.size, 0f, Vector2.down * 1f);
return raycasthit2d.collider !=null;
}
}
在尝试访问 class 本身时,您需要访问 CircleCollider2D class 的 实例 。当您尝试访问 class 时,您只能使用静态方法,因为运行时无法知道您正在尝试访问 class 的哪个实例。首先,您需要在 Start 方法中将碰撞器组件附加到您的播放器,就像您对 Animation
所做的那样
private Animation rotate_anim;
private CircleCollider2D collider;
// Start is called before the first frame update
void Start()
{
rotate_anim = GetComponent<Animation>();
collider = GetComponent<CircleCollider2D>();
}
接下来需要给BoxCast函数提供collider
RaycastHit2D raycasthit2d = Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0f, Vector2.down * 1f);
我正在创建检查地面脚本,但在 Collider2D.bounds 上显示错误 非静态字段、方法或 属性 需要对象引用 'Collider2D.bounds'
有剧本:
public class PlayerScript : MonoBehaviour
{
[SerializeField] float speed = 10f;
private Animation rotate_anim;
// Start is called before the first frame update
void Start()
{
rotate_anim = GetComponent<Animation>();
}
// Update is called once per frame
void Update()
{
Jump();
}
void Jump(){
//not there
}
private bool isGrounded(){
RaycastHit2D raycasthit2d = Physics2D.BoxCast(CircleCollider2D.bounds.center, CircleCollider2D.bounds.size, 0f, Vector2.down * 1f);
return raycasthit2d.collider !=null;
}
}
在尝试访问 class 本身时,您需要访问 CircleCollider2D class 的 实例 。当您尝试访问 class 时,您只能使用静态方法,因为运行时无法知道您正在尝试访问 class 的哪个实例。首先,您需要在 Start 方法中将碰撞器组件附加到您的播放器,就像您对 Animation
所做的那样private Animation rotate_anim;
private CircleCollider2D collider;
// Start is called before the first frame update
void Start()
{
rotate_anim = GetComponent<Animation>();
collider = GetComponent<CircleCollider2D>();
}
接下来需要给BoxCast函数提供collider
RaycastHit2D raycasthit2d = Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0f, Vector2.down * 1f);