How to jump while gravity is reversed
My game is in Javascript. My gravity is normally -9.81 in the Y. My jumpheight is normally 9.
I've successfully flipped the gravity when my player/ball touches a special capsule. Unfortunately, I can't get the ball to jump in the opposite direction: down while I'm on the ceiling.
I have been able to change the jump height when I touch other special capsules that grow and shrink the ball (namely make the ball jump higher or lower) but I can't seem to do the above opposite jump.
Here's my code. Any ideas what I'm doing wrong? I've tried different jumpheight values. #pragma strict
var player : GameObject;
var gravityChangeSfx : AudioClip;
var gravityC = Vector3 (0, 9.81, 0);
function Awake() {
//player = GameObject.Find("Ball").GetComponent.<Rigidbody>();
player = GameObject.Find("Ball");
Debug.Log("GravityUpsideDown script - first Find Player/Ball");
}
function OnTriggerEnter (col : Collider) {
//Trigger if only the player touched the object
//GameObject.Find("Player");
if (col.tag == "Player") {
Debug.Log ("GravityUpsideDown script - ball collided with this Box");
// Change Gravity
Physics.gravity = gravityC;
//Make ball jumpheight the opposite
BallControl.jumpHeight = -9;
Debug.Log("GravityUpsideDown script - jumpHeight made upsidedown");
GetComponent.<AudioSource>().pitch = 1;
GetComponent.<AudioSource>().clip = gravityChangeSfx;
GetComponent.<AudioSource>().Play();
Debug.Log("GravityUpsdieDown script - gravity upside down");
}
}
And here's my ball control code associated with the player ball:
function Update ()
{
//Handle ball rotation.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
//Handle ball jump
if (Input.GetKeyDown(KeyCode.W) && IsGrounded ())
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
}
}
function IsGrounded () : boolean { //Check if we're on the ground, return True if we are, else return Null.
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
I thought the problem had to do with the "&& IsGrounded ()" so I commented that out but it still didn't work.
TIA
Answer by Voodoochief · Jun 08, 2016 at 11:19 PM
First off I would make sure that your Jump is actually being performed. Simply by adding a Debug.Log inside the ball jump statement.
IF that is still being hit I would suggest that you try changing the 'jumpHeight' to '-jumpHeight' to see if that makes the character jump in the desired direction.
Answer by Bwhang · Jun 10, 2016 at 04:55 AM
I must've incorrectly commented out && IsGrounded since it now works correctly.
I just commented out && IsGrounded
Problem now is that I can jump while in mid-air. I need something like IsCeiling ;D
If you change the -Vector3.up, to a Vector3.up when gravity gets reversed you will then detect the ground where it is now (which is above you).
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1); }