Thomas Tom Thomas Tom - 14 days ago 5
C# Question

How to stop a sphere from moving by itself after restarting it's position?

I am on my third day of learning Unity and are currently doing the roll-a-ball tutorial. I am trying to add a restart button, that teleports the ball back to the start-position/origin. It all works good, but the ball will keep moving after I restart, but I want it to stay in a spot and not move by itself.

What I mean is that if I'm moving left at max speed and press 'R' to restart, then it will keep moving left fast for some time, but what I want to achieve is that the ball would not keep moving by itself after I restart it's position (perhaps it's speed is just decreasing so slow that it's unnoticeable, in which case I'd want it to slow down faster).

Here's my code that I just tried out and "hoped" would work, but doesn't. I think that the problem I have here is that a new axisHorizontal/axisVertical variable value is assigned again the next frame, the value being taken from Input.GetAxis, which from what I have heard is not possible to change.

That means that a movementForce bigger than 0/0/0 and AddForce will be activated again the next frame.

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

public float movementSpeed = 7f;
private Rigidbody rigidSphere;

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

void FixedUpdate() {
float axisHorizontal = Input.GetAxis("Horizontal");
float axisVertical = Input.GetAxis("Vertical");

Vector3 movementForce = new Vector3(axisHorizontal, 0.0f, axisVertical);
rigidSphere.AddForce(movementForce * movementSpeed);

if (Input.GetKeyDown(KeyCode.R)) {
transform.position = new Vector3(0, 0, 0);
rigidSphere.AddForce(0, 0, 0);
axisHorizontal = 0;
axisVertical = 0;
}
}
}


I tried googling on how to change the value that is taken from GetAxis, but others claim it's impossible to do so. So I have really no idea how to make it not keep moving forever...

And I also got a 'bonus' question that has arised during those two days that I have been learning Unity, but they are not worth a seperate question, perhaps if they have been already answered before (I've been unable to find any documentation that'd help me on it), then just give me a link on where I could find the answer to my problem.

The question is that how do I make the ball stop moving after some time after I press a button (W/A/S/D, the default values for positive/negative buttons in Input Manager)? For example, I press 'W' just for a second, but the ball will keep moving forward forever and will never stop.

What I want to achieve is after I for example press 'W' for a second, after a few seconds the ball will completely stop, it just seems that the value from GetAxis never goes back to 0. I tried to google about it, and I think it's something to do with Gravity in the Input Manager - I tried changing it, but the ball will still keep moving forever, the only thing I noticed did change was the speed of the ball.

I'd be really grateful if someone could help me with these problems.

Answer

I am trying to add a restart button, that teleports the ball back to the start-position/origin. It all works good, but the ball will keep moving after I restart, but I want it to stay in a spot and not move by itself.

I see that you tried to solve this by using rigidSphere.AddForce(0, 0, 0);. This will only add 0,0,0 to the existing force the ball is moving at. The solution to this is to set the Rigidbody velocity to 0.

Replace rigidSphere.AddForce(0, 0, 0); with rigidSphere.velocity = Vector3.zero;

What I want to achieve is after I for example press 'W' for a second, after a few seconds the ball will completely stop, it just seems that the value from GetAxis never goes back to 0.

Since the ball is rolling, you need to increase the angular drag of the ball. This will make the ball stop rolling after releasing the key. The angular drag is on the Rigidbody attached to the ball. Also increase the drag if that's not enough.

enter image description here