1
\$\begingroup\$

I'm trying to make a game in Unity where the player character is a ball (a sphere), and the game world is a planet (a very large sphere) that the ball can roll around on. The ball can be rolled in any direction, via the arrow keys. If the player holds the right arrow, the ball should roll smoothly to the right, all the way around the planet not leaving the ground, until it eventually reaches back to where it started. The camera follows the ball third person such that the ball always appears in the same position in the screen and the planet appears to be moving underneath it.

My question is, how can I use rigidbody.AddForce() to roll the ball in the way described above? What I think I need to do is to take the vector I get from taking the input values on the horizontal and vertical axes:

float moveX = Input.GetAxis ("Horizontal");
float moveY = Input.GetAxis ("Vertical");

Vector3 inputMovement = new Vector3 (moveX, 0.0f, moveY);

And rotate it somehow, so that I'm applying that vector as force to the ball but in the direction that's tangential to the point on the planet that the ball is currently on, and then apply it as force to the ball's rigidbody. So if you're looking at the planet head on, and the ball is at 3 o'clock, pressing the right arrow key should apply the force directly downwards.

Can anyone help me out here? I think it's probably really simple but I'm having trouble figuring it out exactly.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Subtract the position of the ball from the origin of the planet, that will give you the directional vector that you want. You can calculate the magnitude of the force you need to apply using simple newtonian gravity. Newton Gravity

Convert the direction into a unit vector, and multiply the magnitude of the force by the unit vector to create a force vector.

\$\endgroup\$
2
  • \$\begingroup\$ I don't understand how subtracting the position of the ball from the origin of the planet gives me the direction vector of the force I want to apply to the ball. The planet is centered on (0, 0, 0), so surely subtracting the position of the ball from that will just give me the point on the sphere diametrically opposite to the ball? \$\endgroup\$
    – Jessica
    Commented Jun 13, 2014 at 21:28
  • \$\begingroup\$ @Jessica, you are correct, but that is actually the vector you want. The vector from the centre of the planet to the point opposite the ball is equivalent to the vector from the ball to the centre of the planet. The vector from the ball to the centre of the planet is the direction along which gravity acts upon the ball. \$\endgroup\$
    – Jeff
    Commented Jun 14, 2014 at 0:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .