碰撞后立即停止刚体运动/旋转 [英] Stop Rigidbody Movement/Rotation instantly after collision
问题描述
我希望我的球体从一个位置跳到另一个位置,但不希望它随后平移.我不知道该怎么做.这是我的代码:
I want my sphere to jump from one position to another but don't want it to translate afterward. I can't figure out how to do that. Here's my code:
void Update()
{
if (!thrown && ((Input.touchCount > 0
&& Input.GetTouch(0).phase == TouchPhase.Ended)
|| Input.GetMouseButtonDown(0)))
{
rb.isKinematic = false;
rb.AddForce(new Vector3(0.0f, 15.0f, 5.0f) );
thrown = true;
}
}
推荐答案
有很多方法可以让 Object 在碰撞后立即停止.我给你两种方法:
There are many ways to make Object stop immediately after collison. I will give you two ways:
方法一:
当您检测到碰撞时,将 Rigidbody
的 velocity 设置为 0
.
Set the velocity of the Rigidbody
to 0
when you detect a collison.
如果对象也在旋转,也将 angularVelocity
设置为 0
.
If the object is also rotating, set the angularVelocity
to 0
too.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Rigidbody rbdy = collision.gameObject.GetComponent<Rigidbody>();
//Stop Moving/Translating
rbdy.velocity = Vector3.zero;
//Stop rotating
rbdy.angularVelocity = Vector3.zero;
}
}
方法二:
使用物理材质来控制碰撞过程中的摩擦量.
Use Physic Material to control the amount of friction during collison.
转到资产 > 创建 > 物理材料
将 Bounciness 更改为 0
.
将 Dynamic 和 Static Frictions 更改为等于或大于 1
的值.
Change the Dynamic and Static Frictions to values equals or more than 1
.
然后将其附加到 Collider
上的 Material
插槽.
Then attach it to the Material
slot on your Collider
.
这篇关于碰撞后立即停止刚体运动/旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!