为什么运动学2D刚体对撞机与另一个使用rigdbody2D.cast的运动学2D刚体碰撞机重叠



我正在尝试使用rigidbody2D.cast.Player和地面对撞机重叠进行运动学物体的地面碰撞。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ForceMethod : MonoBehaviour
{
[SerializeField] Vector2 worldGravity;
[SerializeField] float downCastDistance = 0.05f;
Vector2 startingVelocity = new Vector2(0, 0);
RaycastHit2D[] hits = new RaycastHit2D[2];
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
rb.Cast(Vector2.down, hits, downCastDistance);

}
void FixedUpdate()
{
rb.MovePosition(rb.position + startingVelocity * Time.deltaTime);
startingVelocity += worldGravity * Time.deltaTime;
if (hits[0].collider.name == "Ground")
{
rb.MovePosition(rb.position);
}
}
}

之前之后

rb.MovePosition(rb.position);什么也不做,因为将rb移动到当前的相同位置。

你想做的可能是在移动rb之前检查,如果施法击中地面,但也在固定的更新中施法,而不是固定的向下施法距离,但施法距离与rb向下移动的距离相同(即startingVelocity*Time.deltaTime(。如果它没有击中,那么像你一样正常向下,如果它击中了,将rb移动到正常秋季更新和光线投射命中点(位于地面(之间的最高位置:

//FixedUpdate()
float currTravelDistance = startingVelocity.y * Time.deltaTime;
rb.Cast(Vector2.down, hits, Mathf.Abs(currTravelDistance));
if (hits[0].collider.name == "Ground")
{
rb.position.y = Mathf.Max((rb.position + currTravelDistance).y, hits[0].point.y);
}
else {
rb.MovePosition(rb.position + currTravelDistance);
}

同样由于这将rb精确地在地面上移动,如果rb的中心在子画面的中间,则将其向上移动到子画面的"中心"的距离;英尺;到它的中心。最后,如果玩家触地,您还需要重置为0启动速度

最新更新