# Unity - movement code overriding rigidbody mass?

Unity noob here...

I have a basic movement script, but whatever I set the objects mass at it still falls the same speed, floating down off ramps instead of sticking to the floor.

I saw some posts referencing that the movement translate can override the rigidbodys downward velocity. Well I tried several code adjusts but I couldn't get it to work correctly.

float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");

// this code is to keep movement direction when switching to alternate camera
if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical")) {
viewForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
viewRight = Vector3.Scale(Camera.main.transform.right, new Vector3(1, 0, 1)).normalized;
viewRight = new Vector3(viewForward.z, 0f, viewForward.x * -1f);
}

moveDirection = v * viewForward + h * viewRight;
if (moveDirection != Vector3.zero) {
// moveDirection.y = rb.velocity <-- not working
transform.position += moveDirection * Time.deltaTime * moveSpeed;
transform.rotation = Quaternion.LookRotation(moveDirection);
}


Could someone point out what I'm doing wrong?

• If you are using a Rigidbody, you should never write transform.position += ... or transform.rotation = ... — these rip control of the movement out of the physics engine and can cause a number of bugs. Jul 3 '20 at 21:26

whatever I set the objects mass at it still falls the same speed

That's how gravity works. Gravity is a constant rate of acceleration and is not affected by mass. In real life, rate of descent may be slowed by air resistance.

I don't fully understand what you're trying to do with your movement code (move object relative to the camera?), but it doesn't seem like the cleanest way to do what you want. Does the object fall as expected if you aren't giving any horizontal or vertical input?

You might want to reorganize it like this:

float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");

// this code is to keep movement direction when switching to alternate camera
if (Input.GetButtonDown("Horizontal") || Input.GetButtonDown("Vertical")) {
viewForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
viewRight = Vector3.Scale(Camera.main.transform.right, new Vector3(1, 0, 1)).normalized;
viewRight = new Vector3(viewForward.z, 0f, viewForward.x * -1f);

moveDirection = v * viewForward + h * viewRight;
if (moveDirection != Vector3.zero) {
// moveDirection.y = rb.velocity <-- not working
transform.position += moveDirection * Time.deltaTime * moveSpeed;
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}


The mass doesn't affect the speed that has the object when it's falling, but if you want to increase or decrease the air resistance, you have to modify the parameters "Drag" and "Angular Drag" of the rigidbody.

A higher value of this parameters, means, a high air resistance, also, if you want the object to fall with more speed, you can add force downwards at the object, like this:

float force = 100f;