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

2 Answers

2

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.

Yeah I can't use it "Unknown Identifier "parent"" Is there another way?

Getting this error using that: http://image.prntscr.com/image/437e04d5f7004f89a604b9078cf63671.jpeg

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); }

If you want it at weapon1Spawn, just use that: Weapon1 = Instantiate(GunType, Weapon1Spawn.transform); Which is the same as dragging it from your prefabs onto "Weapon1Spawn" in the editor. If you want to make 100% sure it's straight and aligned (it should be) you can then do(might be slightly different in UnityScript): weapon1.transform.localPosition = new Vector3(0,0,0); weapon1.transform.localRotation = Quaternion.identity;

That wasn't something you were supposed to straight out copy, just replace 'parent.transform' with whatever parent transform you wish to use, same goes for the 'prefab' object. In this case you would use; var Weapon1 = Instantiate (GunType, Weapon1Spawn.transform);