# Stopping 3D object in place

I am at 1.2 stage (moving the player) at Unity course "Roll-a-ball" and have question:

I want to add feature that stops a player and sets his new position on 0, 0, 0.

I can reset the position, but I have really problem with stopping a player. The ball won't stop rolling.

My code is:

    using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

private Rigidbody rb;
public float speed = 1.0f;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

if (Input.GetKeyDown(KeyCode.Space))
{
rb.position = new Vector3(0,0.5f,0);
}
print(movement);
}
}


What I tried?

• Adding new vector3 (0,0,0) in if section,
• New vector3 that is opposite to movement,
• .Stop after ascribing new position to (0,0,0), but it 'freezes' before (example: I click space when sphere is at board's edge, then it freezes there, when I click arrow it go to new position and start going).

So, how to do that? I have no clue right now, although problem seems be easy.

Here a image with code and Unity with opened Inspector on object.

EDIT

I've a another problem.

Changed code as below (as Byte56 suggested):

if (Input.GetKeyDown(KeyCode.Space))
{
speed = 0;
rb.velocity = Vector3.zero;
rb.transform.position = new Vector3(0, 0.5f, 0);
rb.constraints = RigidbodyConstraints.FreezeAll;
speed = 1.0f;
}
if (moveHorizontal != 0 || moveVertical != 0)
{
rb.constraints = RigidbodyConstraints.None;
}


It seems like clicing space not always freezes object. It always put it on right place, but sometimes doesn't freeze. No matter if I use code rb.constraints = RigidbodyConstraints.FreezeAll; or rb.Sleep(); - sometimes it works, sometimes not. I can't prove it, but it seems the smaller is delta of time between clicking space (smaller than 1 second) the chance that it won't freeze increases. It's not fault of space, I tried it with NumEnter. Any idea? It seems really buggy.

• I recreated this scene on my own. In about 5 minutes of testing I never encountered a time when pressing space didn't result in the expected result. If you're still having trouble, you should post as a new question, not continuing to edit this one. – MichaelHouse Apr 9 '16 at 20:48

If you want to stop a physics object from moving, you need to zero the forces and the velocity of the RigidBody.

rb.velocity = new Vector3(0,0,0);


For zeroing the force, you can apply a force opposite to the forces you've added that frame already. However, I'd suggest simply not adding those forces if you're stopping. It means a slight rearrangement of your code.

if (Input.GetKeyDown(KeyCode.Space))
{
rb.position = new Vector3(0,0.5f,0);
rb.velocity = new Vector3(0,0,0);
} else {
}


The ultimate way to stop the ball, activating all the constraints on the rigidbody.

if(moveHorizontal != 0 || moveVertical != 0) //for starting the movement again
rb.constraints = RigidbodyConstraints.None;

if (Input.GetKeyDown(KeyCode.Space))
{
rb.gameObject.transform.position = new Vector3(0,0.5f,0);
rb.constraints = RigidbodyConstraints.FreezeAll;
} else {
}


It is due to constantly applying AddForce in FixedUpdate. You can easily do it by assigning zero to speed and set velocity to zero.

rb.AddForce(movement*speed);
if (Input.GetKeyDown(KeyCode.Space))
{
speed = 0;

• As below - it doesn't stop immediately, but slowing and slowing and slowing sphere. The second thing is that setting speed to zero means I can't move this sphere again after clicking 'space'. – Szkaplerny Apr 8 '16 at 17:06
• nah, I didn't think about it, of course you are right. Any idea how to deal with my problem another than .velocity? – Szkaplerny Apr 8 '16 at 17:11