Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.

share|improve this answer
    
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? –  Jessica Jun 13 at 21:28
    
@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. –  Jeff Jun 14 at 0:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.