3

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;
    }
}
5
  • What do you mean "translate"? Do you have more code somewhere translating the ball? If so, you should add it
    – dogiordano
    Apr 2, 2017 at 9:18
  • No,but AddForce is doing the translation.
    – solo365
    Apr 2, 2017 at 9:25
  • If I understood well, you want the ball to stop right after it touches the ground? Apr 2, 2017 at 9:34
  • Yes,that's what I exactly want.
    – solo365
    Apr 2, 2017 at 9:36
  • If both the ball and the ground have Colliders, you could detect collision between the two and freeze the ball's Transform.
    – Hristo
    Apr 2, 2017 at 10:04

1 Answer 1

6

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.

enter image description here

Then attach it to the Material slot on your Collider.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.