# Need help with Physics

I'm using Unity to develop a game. Basically i want the player/rocket to follow a point on the screen where user touches. But It's based on physics. Force must be added when user touches somewhere and the rocket has to reach that point. Gravity is there too. If the rocket passes the point then it has to come by using forces. This is the game http://www.flashyland.com/219-jeux-flash-vol-Space-Oil.html (click the red button on the bottom left side and then green button. Arrow keys to play).

The rocket rotates too. I'm not good in physics at all.

• Did you google anything or look at the Unity docs? docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html Jul 23, 2015 at 20:49
• I know how to add force but I'm not asking for it. It's quite complex movement and i can't figure out how to do it. You can check the link posted Jul 23, 2015 at 21:00
• right now you are using arrow keys? What is this about Basically i want the player/rocket to follow a point on the screen where user touches.? Jul 24, 2015 at 7:27
• that game is not mine. You gonna have to use the keys to play that game. But in my game, the rocket would go where the user touches on the screen. But physics would be the same Jul 24, 2015 at 10:07

I may have interpreted what you want to do incorrectly, but it sounds like what you want to do is have the rocket rotate to look at a point, and then accelerate if the user is touching the screen. I'm not going to help you with the touch input part, but I will assist you in the rotation and actual movement as that seems to be what you asked for

What we need to do first is get the touches position, we need to get it in world space and we want it as a Vector 2. The touch will be in screen space, to convert we can simply run (assuming you have one camera):

Vector3 targetPos = Camera.main.ScreenToWorldPoint(touchPosition);


http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

Now we have the target position we need to figure out the size of the angle needed to point to targetPos

Vector3 dir = targetPos - transform.position;
float rotZ = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;


We call it rotZ because as we are in unity 2d we want to rotate on the z axis.

Next we need to do the actual rotation, for this we need a Rigidbody2D attached. We should access this in the start method of the script

Rigidbody2D RB;
void Start()
{
//Other start work
RB = GetComponent<Rigidbody2D>();
}


Now we have the rigidbody we can do the rotation, we will use the MoveRotation() Method

RB.MoveRotation(-rotZ);


Now the ship is pointing in the right direction we need to add the acceleration, to do this we can use RB.AddForceAtPosition

float Thrust = 5.0f //The amount of force used in the push
RB.AddForceAtPosition(transform.up * Thrust, transform.up);


Hopefully this helps you. I created the post using one of my old scripts, please note that it was used in a hackathon type thing to create a gimmicky control system, so that's why there's calls to retrieve web data. https://github.com/EverlessDrop41/GravityTest/blob/master/Assets/Scripts/Ship/ShipControll.cs

EDIT: After some discussion in chat we figured out increasing drag and thrust then having and overlap circle on the point to stop the acceleration on arrival did the trick

• Thanks! It's exactly what i was looking for. One problem though. Sometimes the rocket has to make too many round trips before reaching the point. Like it keeps running in circles if i place the point somewhere far away or if i change the point quickly. Jul 24, 2015 at 12:11
• When you say place the point, is that done via a touch or via code? Jul 24, 2015 at 12:15
• Touch. But as of right now, I'm using mouse for the touch Jul 24, 2015 at 12:16
• Hmm, when you say going in circles, is that rotating on the spot, or doing loops (if that makes sense?) Jul 24, 2015 at 12:17
• It's not rotating on the spot but going around in loops. It reaches the point eventually but it has to go in circles too many times. Let me make a .gif out of it and show you Jul 24, 2015 at 12:18