All your code is in your previous question. People who see this have no idea what you mean. Lucky for me the questions were still next to each other in the list.
You basically have just one variable changing in your gravity system: the direction of gravity. So you should only have 1 variable to signify it as well. Simpler is better.
var gravityDir = 1;
var gravity = Vector3 (0, -9.81, 0);
var jumpHeight = 9;
...
//when gravity changes, change its sign
gravityDir *= -1;
// after that jus use it as a multiplier
Physics.gravity = gravityDir * gravity;
GetComponent.<Rigidbody>().velocity.y = gravityDir * jumpHeight;
return Physics.Raycast(transform.position, gravityDir * -Vector3.up, distToGround + 0.1);
This way you don’t need any special logic or more variables even if gravity changes back.
Thank you Voodoochief and NoseKills. I’m getting closer to getting this to work using both of your suggestions but it’s not 100% yet.
I’ve got it so that the ball now does jump in the opposite direction (down) when I flip the gravity. However, the test to make sure you’re on the ground or ceiling isn’t working so you can jump again while in mid-air.
I got to where I am now by making the -Vector3.up into a variable and ditching two parenthesis which I don’t quite understand but it helped.
I ditched the “-Vector3.up” in my IsGrounded function and made it the variable (grav) so I could change it from the new default of
Vector3.down
to
Vector3.up
when the player/ball touches the special pill that inverts gravity.
function IsGrounded () : boolean { //Check if we're on the ground, return True if we are, else return Null.
Debug.Log("BallControl script - before IsGrounded raycast " + grav);
var grav = Vector3.down;
return Physics.Raycast(transform.position, grav, distToGround + 0.1);
I also ditched two parenthesis “()” after IsGrounded and that made the player/ball jump while on the ground and on the ceiling. Not sure why this worked but it does:
//Handle ball jump
//if (Input.GetKeyDown(KeyCode.W) && IsGrounded()) //Ditched the two parenthesis and now it works
if (Input.GetKeyDown(KeyCode.W) && IsGrounded)
{
Debug.Log("BallControl script - W was pressed");
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
Now I just have to make sure the player/ball can only jump while it is in contact with the ground and/or ceiling and not while in mid-air.
Just realized that ditching the two parenthesis negates the ground check so that's why I'm jumping in mid-air. By leaving the parenthesis in, I wasn't jumping at all. Guess I'll have to add those parenthesis back in. 06/27/2016: Thank you Voodoochief for pointing out that I had a "var" that was unnecessary. Once I ditched that, it all works now. I only jump now when I'm on the ground or ceiling depending on how gravity is flipped.
Just realized that ditching the two parenthesis negates the ground check so that's why I'm jumping in mid-air. By leaving the parenthesis in, I wasn't jumping at all. Guess I'll have to add those parenthesis back in. 06/27/2016: Thank you Voodoochief for pointing out that I had a "var" that was unnecessary. Once I ditched that, it all works now. I only jump now when I'm on the ground or ceiling depending on how gravity is flipped.
– Bwhang