Question by
Serah62 · Oct 05, 2020 at 12:21 AM ·
scripting problemmovementgravityphisics
local Physics
I'm trying to change gravity for a game object that is controlled by the player so when it hits a box collider the players character changes rotation and gravity along with what way is forward left right etc. I had some success with using reycasts but the gravity of everything in the world changed so I thought id try triggers and colliders I've been working on this for about 3 weeks any help would be very welcomed and appreciated.
public class ChangeGravity : MonoBehaviour
{
public static float globalGravity = -9.81f;
public float gravityScale = 10f;
private Rigidbody rb;
// Start is called before the first frame update
void OnEnable()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
}
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 gravity = globalGravity * gravityScale * Vector3.up;
rb.AddForce(gravity, ForceMode.Acceleration);
//Physics.gravity = new Vector3(0, 10, 0);
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
gravityScale *= -1;
}
}
}
Comment
Hmmm...the variable is named globalGravity and it's static. Those match-up: it's everyone's shared gravity. But in the question it sounds as if you want it to be only for that one gameObject. Everyone can have their own personal up/down gravity. But you named it globalGravity... hmmm.