'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;
}
}
Solution 1:[1]
There are many ways to make Object stop immediately after collison. I will give you two ways:
Method 1:
Set the velocity of the Rigidbody
to 0
when you detect a collison.
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;
}
}
Method 2:
Use Physic Material to control the amount of friction during collison.
Go to Assets > Create > Physic Material
Change the Bounciness to 0
.
Change the Dynamic and Static Frictions to values equals or more than 1
.
Then attach it to the Material
slot on your Collider
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |