统一 如何相对于其角度将一个对象与另一个对象"power bounce"?



我正在做一个简单的3D游戏,其中一些球(固定的Z位置(沿着路径(使用重力和物理材料(落到一个小的平坦平台上,并从这个平台上"反弹"。玩家可以旋转这个平台,所以我想根据平台的角度重新创建一个逼真的弹跳方向。

我是编码新手,但到目前为止,我已经弄清楚了球与平台碰撞时的矢量与平台法线之间的关系,法线应该是与表面垂直的线,可用于将球的矢量反射到另一个方向。

我已经使用 OnCollisionEnter 和 if 语句来检测它是否是您与之碰撞的平台,但我不明白在哪里指示表面的法线以及如何访问它。它应该作为另一个对象中的公共类,还是可以从球赛对象中检测到?

我尝试了这个网站和其他网站上的一些例子,并走到了这一步:

public class OnCollision : MonoBehaviour
{
public float speed = 25f;
public Rigidbody rb;
private Rigidbody rigid;
private void Start()
{
rigid = transform.GetComponent<Rigidbody>();
}
private void OnCollisionEnter(Collision collision)
{
if (collision.transform.tag == "BouncePad") {
rb.velocity = transform.up * speed;
}
}
}

现在它垂直反弹,所以我猜我应该更改 transform.up * speed 部分所在的代码。

请问谁能指导我?

非常感谢。

如果您已经在使用物理材质,请查看弹性属性。值为 0 表示没有反弹,值为 1 将导致能量不损失。将为您计算反弹角度。确保将物理材质拖到每个对象上 - 球和墙壁的材质都会产生影响。

最后有人帮了我一把,得出了这个解决方案:

public class Bounce : MonoBehaviour
{
public Rigidbody rb;
public float str = 0.21f;
public float str2 = 0.15f;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "BouncePad")
{
rb.AddForce(rb.velocity * str, ForceMode.Impulse);
}
if (col.gameObject.tag == "BouncePad2")
{
rb.AddForce(rb.velocity * str2, ForceMode.Impulse);
}
}
// Update is called once per frame
void Update()
{
}

}

public class BouncTest : MonoBehavior {

[SerializeField] private float hight = 3;
[SerializeField] private int times = 5;
[SerializeField] private float speed = 8;
private Vector3 _startPos;
private bool _checkUP;
private int _countTimes;
private float _hightbuf;
[HideInInspector]
public bool _bounceEnd;
private void Awake()
{
_startPos = transform.position;
}
public void TurnOnBounceEffect()
{
_bounceEnd = true;
_checkUP = false;
_hightbuf = hight;
_countTimes = 0;
}
private void FixedUpdate()
{
BounceEffect();
}
private void BounceEffect()
{
if (_bounceEnd)
{
if (!_checkUP)
{
if (transform.position.y <= (_startPos.y + _hightbuf))
transform.position = Vector2.MoveTowards(transform.position, new Vector2(_startPos.x, transform.position.y) + (Vector2.up * _hightbuf), speed * Time.fixedDeltaTime);
else
{
_checkUP = true;
}
}
else if (times != _countTimes)
{
if (transform.position.y > _startPos.y)
transform.position = Vector2.MoveTowards(transform.position, _startPos, speed * Time.fixedDeltaTime);
else
{
_countTimes++;
_checkUP = false;
_hightbuf /= 2;
}
}
else
{
transform.position = Vector2.MoveTowards(transform.position, _startPos, speed * Time.fixedDeltaTime);
if (transform.position.y <= _startPos.y)
{
_bounceEnd = false;
}
}
}
}

}

最新更新