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?
transform.position += ...
ortransform.rotation = ...
— these rip control of the movement out of the physics engine and can cause a number of bugs. \$\endgroup\$ – DMGregory♦ Jul 3 '20 at 21:26