Help with better movement

I’m learning Unity from scratch, following the tutorials. I’ve already completed Roll a Ball, and I’m almost done with Space Shooter. However, I noticed the movement of my player to feel unnatural. The player never stops when I release the key, its like it starts to slow down, instead of just stopping. How could I make the player instantly stop the moment I release the key?

Paste your code (but make sure to insert it as code).

But usually it goes like this:

var speed : int = 30; 

if (Input.GetKey (KeyCode.W)) {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);       
        rb.AddForce(movement * speed);

When you add force, time to stop it depends of materials of colliders, your player collider and your Terrain collider have default Phishic materials.

https://docs.unity3d.com/Manual/class-PhysicMaterial.html

You can create new physics material without friccion, or use

rb.velocity = movement * speed

whit velocity you dont need phisics materials, because if Vector 3 movement its 0, your velocity will be 0 too, and stop inmediatly.

I recommend you to normalice your vector3 movement, I think when you moves only horizontal or only vertical, your movement will be slowler than moving horizontal + vertical

To fix that:

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

// This can work fine:

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical).normalized * speed;
rb.velocity = movement;

Maybe, you need now change valor of speed, to adjust to new parameters, but, its good routine use normalize vectors and use Time.deltaTime to fix problems and get estable vectors and numbers.

1 Like

That worked like a charm, thanks so much.

But back to the addForce method, how could I make my ball bounce back once it collides with a wall?

The Physic materials have an option related with bounce. Yo need to learn about that on manual , and configure your wall collider material and your ball collider material , and test results i cant say you what numbers you need, mass, drag of rigidbody and gravity can affect to result, im not sure, im not tested much.