- Home /
Gravity switch issues
I am having a really odd issue with a gravity switch I made. At first it was working perfectly--the player collided with it, it lowered gravity from 50 to 20 (handcoded, simple gravity) but I just went back to it today and even though I can see the gravity variable changing in the Inspector, the player that the gravity is affecting still has the same jump height and fall speed. Any insights as to why this is occurring would be appreciated.
The code for the gravity switch is:
using UnityEngine;
using System.Collections;
public class GravitySwitchScript : MonoBehaviour
{
public bool isSwitched=false;
public float timeDelay=5f;
public Player player;
public Material originalMaterial;
public DestroyEverythingScript destroyer;
void OnTriggerEnter ()
{
if (timeDelay>0)
{
isSwitched=true;
}
}
void Update ()
{
if (isSwitched==true)
{
renderer.material.color=Color.green;
player.gravity=20;
timeDelay-=Time.deltaTime;
}
if (timeDelay<0)
isSwitched=false;
if (isSwitched==false)
{
player.gravity=50;
renderer.material.color=originalMaterial.color;
timeDelay=5f;
}
if (destroyer.isDestroying)
Destroy (this);
}
}
And the jump function for the player (where gravity is interpreted) is this:
//controls the character's jumping. takes the character controller as a parameter.
void JumpFunc (CharacterController controller)
{
//this resets the doubleJump counter and the yMovement if the player is on the ground
if(controller.isGrounded)
{
yMovement=0;
doubleJump=0;
//this jumps once if the player presses space, regardless of double jump counter.
if(Input.GetKeyDown(KeyCode.W))
{
yMovement = jumpSpeed;
doubleJump=1;
}
}
//this is the double jump--as long as there hasnt already been two jumps, you can jump again using spacebar.
else if (Input.GetKeyDown (KeyCode.W) && doubleJump<2)
{
doubleJump=2;
yMovement = jumpSpeed;
}
//this applies gravity to the yMovement variable
yMovement -= gravity * Time.deltaTime;
//this makes the yMovement variable affect the moveDirection vector
moveDirection.y=yMovement;
//this applies the moveDirection vector to the character controller so the player moves appropriately.
controller.Move(moveDirection * Time.deltaTime);
}
Any help would be appreciated.
Your answer
Follow this Question
Related Questions
Gravity switch not working 0 Answers
object gravity issues 0 Answers
Changing gravity direction... 5 Answers
Anti-gravity is turning on when it shouldn't 1 Answer
CharacterController.Move uses gravity even though it shouldn't 1 Answer