unity OnTriggerEnter2D 高速击球落地不起作用
Unity OnTriggerEnter2D hit the ball to floor on high speed doesn't work
当我设置 speed
of Ball = 10 OnTriggerEnter2D
来测试击中地板的球是否正常,但是当我将 speed
设置得更高 (20) 时,OnTriggerEnter2D
不起作用,球通过 floor
落下
我的代码:
void Start () {
rigiBody = GetComponent<Rigidbody2D>();
ballLayer = 1 << LayerMask.NameToLayer("Ball");
}
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag(Constants.FLOOR_TAG))
{
Debug.Log("FLOOR_TAG");
if (HitFloor != null)
HitFloor(this);
}
}
void FixedUpdate() {
Vector2 tempVect = Direction;
tempVect = tempVect.normalized * Speed * Time.deltaTime;
Vector2 newPos = rigiBody.position + tempVect;
rigiBody.MovePosition(newPos);
timer += Time.deltaTime;
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * Time.deltaTime * 1.2f, ~(ballLayer));
if (!hit)
return;
...
下面Ball
的督察
这段代码有什么问题?
ps 我使用的是 Unity 2017.1.1f1 个人版
将Rigidbody2D组件中的碰撞检测模式设置为Continuous。 Documentation
也许会改变
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * Time.deltaTime * 1.2f, ~(ballLayer));
至
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * 1.2f, ~(ballLayer));
也会解决这个问题。
为什么要从 newPosition 投射光线?伊莫。您应该从当前位置施放它。
你必须改变刚体的"Collision Detection" 属性。应该是 "continuous" 而不是 "discrete"。如果您选择离散值,那么您就是在告诉刚体在离散时间间隔内检查碰撞。如果你高速移动,刚体可能会错过碰撞。
我的问题的解决方案非常接近
添加并更改了几行
- 为 class 添加
[RequireComponent(typeof(Rigidbody2D))]
Ball
- 将变量
RigiBody
替换为 属性
现在一切正常,球不会高速掉落
更改后的代码如下所示
[RequireComponent(typeof(Rigidbody2D))] // Added this code
public class Ball : MonoBehaviour {
private Rigidbody2D _rigiBody;
public Rigidbody2D RigidBody { //And this property
get {
if (_rigiBody == null)
_rigiBody = GetComponent<Rigidbody2D>();
return _rigiBody;
}
}
当我设置 speed
of Ball = 10 OnTriggerEnter2D
来测试击中地板的球是否正常,但是当我将 speed
设置得更高 (20) 时,OnTriggerEnter2D
不起作用,球通过 floor
我的代码:
void Start () {
rigiBody = GetComponent<Rigidbody2D>();
ballLayer = 1 << LayerMask.NameToLayer("Ball");
}
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag(Constants.FLOOR_TAG))
{
Debug.Log("FLOOR_TAG");
if (HitFloor != null)
HitFloor(this);
}
}
void FixedUpdate() {
Vector2 tempVect = Direction;
tempVect = tempVect.normalized * Speed * Time.deltaTime;
Vector2 newPos = rigiBody.position + tempVect;
rigiBody.MovePosition(newPos);
timer += Time.deltaTime;
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * Time.deltaTime * 1.2f, ~(ballLayer));
if (!hit)
return;
...
下面Ball
的督察
这段代码有什么问题?
ps 我使用的是 Unity 2017.1.1f1 个人版
将Rigidbody2D组件中的碰撞检测模式设置为Continuous。 Documentation
也许会改变
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * Time.deltaTime * 1.2f, ~(ballLayer));
至
RaycastHit2D hit = Physics2D.Raycast(newPos, Direction, Speed * 1.2f, ~(ballLayer));
也会解决这个问题。
为什么要从 newPosition 投射光线?伊莫。您应该从当前位置施放它。
你必须改变刚体的"Collision Detection" 属性。应该是 "continuous" 而不是 "discrete"。如果您选择离散值,那么您就是在告诉刚体在离散时间间隔内检查碰撞。如果你高速移动,刚体可能会错过碰撞。
我的问题的解决方案非常接近
添加并更改了几行
- 为 class 添加
[RequireComponent(typeof(Rigidbody2D))]
Ball
- 将变量
RigiBody
替换为 属性
现在一切正常,球不会高速掉落
更改后的代码如下所示
[RequireComponent(typeof(Rigidbody2D))] // Added this code
public class Ball : MonoBehaviour {
private Rigidbody2D _rigiBody;
public Rigidbody2D RigidBody { //And this property
get {
if (_rigiBody == null)
_rigiBody = GetComponent<Rigidbody2D>();
return _rigiBody;
}
}