Hi,
In my game I have a player object who runs around a sphere (‘planet’). To do this I’ve implemented a very basic ‘gravity’ script that just applies a constant force to the player object every fixed update:
void FixedUpdate()
{
//From the gravity source to the player to get right direction
Vector3 direction = (gravitySource.position - transform.position).normalized;
Vector3 acceleration = direction * g;
rb.AddForce(acceleration, ForceMode.Acceleration);
}
In my player script, I grab the input, check that it’s not zero then apply it as a force to the players rigidbody:
private void move()
{
float horizontalAxis = Input.GetAxis("Horizontal");
float vertcialAxis = Input.GetAxis("Vertical");
float delta = 0.02f;
bool applyMove = (horizontalAxis>0.0f+delta) || (vertcialAxis>0.0f+delta);
moveDirection = new Vector3(horizontalAxis, 0.0f, vertcialAxis);
Vector3 move = moveDirection*Time.deltaTime*MoveSpeed;
if( applyMove)
{
rigidbody.AddRelativeForce(move);
}
}
My problem:
When the player stops pressing one of the input keys, I’d like the player object to stop moving.
So far I’ve tried setting the rigidbody’s velocity to zero, but because this happens every fixedUpdate the player is’t pressing a key it stops the force being applied from the gravity script. I have also read that it is a bad idea to set a rigidbodys velocity every fixed update.
I’ve tried adding a counteracting force when the player isn’t pressing a key like:
if( applyMove)
{
rigidbody.AddRelativeForce(move);
last_force = move;
}
else
{
if(last_force.sqrMagnitude > 0+delta)
{
rigidbody.AddRelativeForce(-rigidbody.velocity);
}
last_force = new Vector3(0,0,0);
}
However this seems to have little effect.
I’ve also tried using rigidbody.MovePosition(). This has two problems, 1) because you’re moving to an absolute position, the force applied due to the gravity script gets ignored so I added a movement due to gravity to the vector I passed to MovePosition which led to 2) my player object seem to constantly be bouncing in and out of the surface of the sphere it was resting on.
What am I doing wrong here? Should I be using rigidbody.addrelativeforce to move my player object? should I be using moveposition? If I use move position how do I stop the player object from ‘bumping into’ what he’s standing on? If I use addrelativeforce, how do I slow my player object down sensibly?
Any thoughts or comments would be appreciated.
Cheers!