- Home /
How to ensure player rock will roll?
Hello, I'm learning Unity by making a prototype where you play as a rock, I managed to get the desired player movement with Rigidbody.AddForce, but there is a minor inconvenience I would like to fix, because of the shape of the rock sometimes addForce will not apply enough force to make the rock roll, see this behavior on the gif.
https://imgur.com/MOanMnt
I'm pressing A to make the rock move left throughout the gif.
I could just increase the applied force or change the mass, but then the rock would not feel as heavy as I would like.
So how should I ensure the rock will roll without making it move too fast?
Answer by DenisIsDenis · Aug 17, 2021 at 03:04 AM
In your case, it is better to use rigidbody.angularVelocity
instead of rigidbody.AddForce()
. Here is an example of the movement script:
using UnityEngine;
public class RollRock : MonoBehaviour
{
Rigidbody rockRigidbody;
Vector3 movement;
void Start()
{
rockRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
movement = Vector3.right * Input.GetAxisRaw("Vertical")
- Vector3.forward * Input.GetAxisRaw("Horizontal");
if (movement != Vector3.zero)
{
rockRigidbody.angularVelocity = movement.normalized * 5;
}
}
}
Should also be assigned to the stone collider physical material with high friction values to avoid slippage.
I'd like to chime in here and note that the equivalent of Rigidbody.AddForce() for angularVelocity is Rigidbody.AddTorque().
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Object move on z axis while giving force on x axis 0 Answers
Knock down moving target ridigbody from collision. 1 Answer
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Rigidbody2D adding force vs modifying velocity for character jump 1 Answer