How to make a player instantly reach to a change in gravity?
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.
I accidentally didn't finish what I wanted to say. I want to make is so that as soon as the gravity changes, the player instantly reacts to the changes in gravity.
Answer by SirWaffleGamin · Nov 19, 2015 at 07:02 PM
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
}
Answer by jonofarc · Nov 10, 2015 at 10:02 PM
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 added what you told me to my code, but it doesn't work. I'm still having the same problem. Is it because the prefab has a setting for stick to ground force and it is always changing to that value?
Also this is my code:
private void InputHandle()
{
if (Input.Get$$anonymous$$ouseButtonUp($$anonymous$$eyCode.$$anonymous$$ouse0)
{
GetComponent<FirstPersonController> ().m_$$anonymous$$oveDir = Vector3.zero;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
}
else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad8))
{
Physics.gravity = Vector3.up * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
/*Quaternion deltaRotation = Quaternion.Euler(AngleVelocity * Time.deltaTime);
m_RigidBody.$$anonymous$$oveRotation(m_RigidBody.rotation * deltaRotation);*/
}
else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad4))
{
Physics.gravity = Vector3.left * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
}
else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad5))
{
Physics.gravity = Vector3.down * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Escape))
{
Application.Quit();
}
}
This works for me I made some $$anonymous$$or changes to your code and it works for me
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class gravityuser : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonUp(0) )
{
Physics.gravity = Vector3.up * 10;
GetComponent<FirstPersonController> ().m_$$anonymous$$oveDir = Vector3.zero;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad8))
{
Physics.gravity = Vector3.up * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 0;
/*Quaternion deltaRotation = Quaternion.Euler(AngleVelocity * Time.deltaTime);
m_RigidBody.$$anonymous$$oveRotation(m_RigidBody.rotation * deltaRotation);*/
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad4))
{
Physics.gravity = Vector3.left * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad5))
{
Physics.gravity = Vector3.down * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
}
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.$$anonymous$$eypad6))
{
Physics.gravity = Vector3.right * 10;
GetComponent<FirstPersonController> ().m_StickToGroundForce = 10;
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Escape))
{
Application.Quit();
}
}
}
Your answer
Follow this Question
Related Questions
Adding Gravity to a game object to make a black hole sucking effect. 1 Answer
How can I bind this script into the state of my camera? 2 Answers
WheelColliders Slip without end (with planet gravity, custom terrain mesh) 1 Answer
I need to add a jump function and a gravity function to this script, but I am stuck. 0 Answers
Object refuses to collide 0 Answers