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.
Are you sure you've cleanly dropped in the code? I don't think this will compile. As it stands, the match for 'if' statement on line 1 is the closing bracket at line 51. This leaves an unmatched 'else' on line 27. Note your lack of consistent indentation makes figuring out this code difficult. But I'm guessing that the 'if' on line 1 should be matched to the 'else' on line 27 so that the gravity of planets is applied regardless of whether the left mouse button is held down or not.
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.
Robertbu, you sir. You are a genius, I can't believe I didn't see that.
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.
Your new structure has the planets only being applied if the mouse is not down and there is fuel. Not sure if that is the way you want it. I would remove the 'else' and its brackets and delete line 54. That is planetary gravity should be applied every frame. But that does not figure out why you are not seeing any effect. You need tp place a Debug.Log() around line 79 to take a look at the value of sumVect and velocity...make sure the ratio of the two is about what you would expect for different planetary distances.
Are you sure you've cleanly dropped in the code? I don't think this will compile. As it stands, the match for 'if' statement on line 1 is the closing bracket at line 51. This leaves an unmatched 'else' on line 27. Note your lack of consistent indentation makes figuring out this code difficult. But I'm guessing that the 'if' on line 1 should be matched to the 'else' on line 27 so that the gravity of planets is applied regardless of whether the left mouse button is held down or not.
– robertbuyoure right, the actual code from that line should be and is this... { if(fuel > 0){ burn = false; } if(fuel > 0.0f){ but still....
– TSI25where do you get fuel <= 0.0 ? right now its set to be my refueling boolean which doesnt actually have anything to do with fuel amount.
– TSI25