2d space gravity?

So I’m trying to make a 2d space game in which the player flies around by clicking on the screen. the ship automatically looks where ever the player has clicked and gains momentum in that direction for as long as the click is held down BUT if they have a lot of momentum to the left, and they merely tap right, they’ll look right but still be traveling left. hopefully that makes sense.

THAT all works perfectly well, what is NOT working is gravity. The ship is flying in between a lot of planets, and the ship should be pulled toward those planets by a gravitational factor. For some reason, right now, the ship is completely unaffected by gravity. Can anyone help me figure out why? The code is in Javascript and here it is.

    if(Input.GetMouseButton(0) && refueling == false)
    {
     if(fuel < 0){
     burn = false; 
     } else { 
     	
     	for (var j = 0; j < 4; j++)
        {
            
		gravityVect.x = planets[j].transform.position.x - ship.transform.position.x;
		gravityVect.y = planets[j].transform.position.y - ship.transform.position.y;
		gravityVect.z = planets[j].transform.position.z - ship.transform.position.z;
		gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
		sumVect.x += gravityVect.x;
		sumVect.y += gravityVect.y; 
		sumVect.z += gravityVect.z;
    }
    
		var pos = Input.mousePosition;
		pos.z = Camera.main.transform.position.y;
		pos = Camera.main.ScreenToWorldPoint(pos);
		var targetRotation = Quaternion.LookRotation(pos - transform.position);
		velocity += transform.forward * Time.deltaTime * force;
		fuel--;
		burn = true;
    
} else {
    
    for (var k = 0; k < 4; k++)
        {
            
		gravityVect.x = planets[k].transform.position.x - ship.transform.position.x;
		gravityVect.y = planets[k].transform.position.y - ship.transform.position.y;
		gravityVect.z = planets[k].transform.position.z - ship.transform.position.z;
    
    	gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
    
		sumVect.x += gravityVect.x;
		sumVect.y += gravityVect.y; 
		sumVect.z += gravityVect.z;
    
    
    }
    }
    //actually moves the ship

    }
    sumVect = sumVect * Time.deltaTime;
    velocity.x = velocity.x + sumVect.x;
    velocity.y = velocity.y + sumVect.y;
    velocity.z = velocity.z + sumVect.z;
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    
    transform.position += velocity * Time.deltaTime * 70.5;

With the addition of the line you specify, here is your code correctly indented:

#pragma strict

function Update()
{
	if(Input.GetMouseButton(0) && refueling == false)
	{
		if(fuel < 0)
		{
			burn = false; 
		} 
		if(fuel > 0.0f)
		{ 
			for (var j = 0; j < 4; j++)
			{
			       gravityVect.x = planets[j].transform.position.x - ship.transform.position.x;
			       gravityVect.y = planets[j].transform.position.y - ship.transform.position.y;
			       gravityVect.z = planets[j].transform.position.z - ship.transform.position.z;
			       gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
			       sumVect.x += gravityVect.x;
			       sumVect.y += gravityVect.y; 
			       sumVect.z += gravityVect.z;
			}
	 
	       var pos = Input.mousePosition;
	       pos.z = Camera.main.transform.position.y;
	       pos = Camera.main.ScreenToWorldPoint(pos);
	       var targetRotation = Quaternion.LookRotation(pos - transform.position);
	       velocity += transform.forward * Time.deltaTime * force;
	       fuel--;
	       burn = true;
		}
		else // fuel <= 0.0
		{
		    for (var k = 0; k < 4; k++)
	        {
		       gravityVect.x = planets[k].transform.position.x - ship.transform.position.x;
		       gravityVect.y = planets[k].transform.position.y - ship.transform.position.y;
		       gravityVect.z = planets[k].transform.position.z - ship.transform.position.z;
		 
		        gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
		 
		       sumVect.x += gravityVect.x;
		       sumVect.y += gravityVect.y; 
		       sumVect.z += gravityVect.z;
		    }
	    }
	    //actually moves the ship
	}
    sumVect = sumVect * Time.deltaTime;
    velocity.x = velocity.x + sumVect.x;
    velocity.y = velocity.y + sumVect.y;
    velocity.z = velocity.z + sumVect.z;
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
 
    transform.position += velocity * Time.deltaTime * 70.5;
}

As you can see, the ‘else’ that contains all of your planetary gravity code matches the ‘if (fuel > 0.0)’. That means your planetary code will only be executed if fuel <= 0.0. In addition, all of this code is inside the ‘GetMouseButton(0)’, so it will only be executed if the mouse button is down.

So, I made the changes recommended by robertbu but the gravity is STILL not affecting the movement of the ship. This is the code that i have currently, i tried to make it indented and i added comments to help you guys read it. Help?

if(refueling == false)
	{//<!-----------ALPHA------------------!>
	
		if(Input.GetMouseButton(0) && fuel > 0.0f)
		
			{//<!-------------------BRAVO-----------------------------------------!>
			
			//This is the case where refueling is false, the player has fuel, and the mouse is clicked. The players movement
			 //is determined by previous velocity, an input acceleration vector, and gravitational pull.				
					for (var j = 0; j < 4; j++)
					
					{//<!---------------CHARLIE--------------!>
					
						//Collects the player's distance from each planet
						gravityVect.x = planets[j].transform.position.x - ship.transform.position.x;
						gravityVect.y = planets[j].transform.position.y - ship.transform.position.y;
						gravityVect.z = planets[j].transform.position.z - ship.transform.position.z;
						
						//creates a gravity vector representing the planet's distance from the player
						gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
						
						//adds the gravity vector to a 'sum vector' representing the pull of all planets
						sumVect.x += gravityVect.x;
						sumVect.y += gravityVect.y;
						sumVect.z += gravityVect.z;
						
					}//</ --------------------CHARLIE---------------->
					
 				//detects where on the screen the player has clicked.
				var pos = Input.mousePosition;
				pos.z = Camera.main.transform.position.y;
				pos = Camera.main.ScreenToWorldPoint(pos);
				
				//determines where the player's ship should rotate to look.
				var targetRotation = Quaternion.LookRotation(pos - transform.position);
				
				// determines the players velocity based entirely on clicking
				velocity += transform.forward * Time.deltaTime * force;
				
				//subtracts fuel every frame, and plays the 'burn' noise
				fuel--;
				burn = true;
				
				//makes the sumVect created earlier into an actual acceleration vector
				sumVect = sumVect * Time.deltaTime;
				
				//adds the sumVect to the player's acceleration vector
				velocity.x += sumVect.x;
				velocity.y += sumVect.y;
				velocity.z += sumVect.z;
				
				//this code actually moves the player through space, and allows the player to rotate.
				transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
				transform.position += velocity * Time.deltaTime * 70.5;
				
				
				}//</ ------------BRAVO-----------------------------------------------------> 
				
				else {//<!-------------------DELTA------------------------------>
				
						//This is the case where refueling is false and the mouse is not clicked.
						//The players movement is determined by previous velocity, and gravitational pulls alone.
						for (var k = 0; k < 4; k++)
						
							{//<!---------------EPSILON----------------->
							
							   //Collects the player's distance from each planet   
								gravityVect.x = planets[k].transform.position.x - ship.transform.position.x;
								gravityVect.y = planets[k].transform.position.y - ship.transform.position.y;
								gravityVect.z = planets[k].transform.position.z - ship.transform.position.z;
								
								//creates a gravity vector representing the planet's distance from the player
								gravityVect = (gravityVect.normalized * gConstant / gravityVect.sqrMagnitude);
								
								//adds the gravity vector to a 'sum vector' representing the pull of all planets
								sumVect.x += gravityVect.x;
								sumVect.y += gravityVect.y;
								sumVect.z += gravityVect.z;
								
							}//</ ---------------EPSILON---------------->
						
						//makes the sumVect created earlier into an actual acceleration vector
						sumVect = sumVect * Time.deltaTime;
									
						//sets the player's current acceleration vector to the sumVect		
						velocity.x = sumVect.x;
						velocity.y = sumVect.y;
						velocity.z = sumVect.z;
						
						//actually moves the player through space.
						transform.position += velocity * Time.deltaTime * 70.5;
						
						}//</ ------------------DELTA-------------------------------->

    }//</ ---------------ALPHA-------------------->

I have literally no idea why gravity isn’t taking effect at this point. The gConstant is 1000, and the ship moves just fine - there just isnt any gravity.