I am making a game where the player can change the position of gravity. Objects instantly react to the change, but the player doesn’t. The player only reacts to gravity going up after the player has jumped, and only reacts to gravity going down after an uncertain amount of time. I want to make it so that as soon as the gravity changes, Here is the code on gravity switching:
using UnityEngine;
using System.Collections;
public class ChangeGravity : MonoBehaviour {
// Use this for initialization
void Start () {
Physics.gravity = new Vector3(0, -1, 0);
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.UpArrow)) {
Physics.gravity = Vector3.up * 5;
}
else if(Input.GetKeyUp(KeyCode.DownArrow)) {
Physics.gravity = Vector3.down * 5;
}
}
}
I have a video as an example but I will add it as a YouTube link if you need it.
@SirWaffleGamin
The problem is that the FPSController that you are using reacts diferent than a normal rigid body so what you can do its:
first we modify the FirstPersonController script on line 18 we make m_StickToGroundForce a public variable instead of private
[SerializeField] public float m_StickToGroundForce
instead of
[SerializeField] privatefloat m_StickToGroundForce
we do the same with m_MoveDir on line 35
public Vector3 m_MoveDir = Vector3.zero;
instead of
private Vector3 m_MoveDir = Vector3.zero;
once done we can acces those variable from an outside script like yours then you just need to add some minor changes
using UnityEngine;
using System.Collections;
// we use firstPerson controller from standar assets
using UnityStandardAssets.Characters.FirstPerson;
public class ChangeGravity : MonoBehaviour {
//we create the variable so we can acces FirstPersonController script
FirstPersonController FPScontroller;
// Use this for initialization
void Start () {
Physics.gravity = new Vector3(0, -1, 0);
FPScontroller = GetComponent<FirstPersonController>();
}
// Update is called once per frame
void Update () {
/
if(Input.GetKeyUp(KeyCode.UpArrow)) {
Physics.gravity = Vector3.up * 3;
//we stop all movement from FPSController
FPScontroller.m_MoveDir=Vector3.zero;
// we removeS stick to ground force wich was the force keeping us in ground untill we jumped
FPScontroller.m_StickToGroundForce=0;
}
else if(Input.GetKeyUp(KeyCode.DownArrow)) {
Physics.gravity = Vector3.down * 3;
//we restore m_StickToGroundForce so we can walk on ground again
FPScontroller.m_StickToGroundForce=10;
//we stop all movement from FPSController
FPScontroller.m_MoveDir=Vector3.zero;
}
}
}
this script goes into de FPSController prefab
I found out the answer. If you want to make it so that the player automatically moves when there is a change in gravity, all you have to do set the m_StickToGrondForce = 0 in the Update method.
Like this:
private void Update()
{
GetCommponent<FirstPersonController> ().m_StickyToGroundForce = 0;
// This is where you can do everything else
}