How to change the player gravity level?,How to change the value of the players gravity?
The game I'm currently working on revolves around changing the value of the players Gravity Down Force. At the start it's set to 10, but I want to make a trigger where, when activated (box/button style), changes the value to 5. I have some code already written and when added to the trigger I can change the value, but when I activate it nothing happens. Here's what I wrote:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gravity : MonoBehaviour {
void OnTriggerEnter(Collider other)
{ }
public float gravityDownForce = 5;},The game I'm working on revolves around changing the players gravity. At the game start the Gravity Down Force is set to 10, but I want to make a trigger that when activated (through box/button) it changes the value to 5. This is what I have so far but it doesn't work when I activate the trigger.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gravity : MonoBehaviour {
void OnTriggerEnter(Collider other)
{ }
public float gravityDownForce = 5;}
Answer by Saiansh2525 · May 14, 2020 at 10:07 PM
If you want to change the gravity, inside the OnTriggerEnter(Collider other) box just type in physics.gravity = gravityDownForce;. I believe that this works and I still have to try it. Tell me whether that works or not. Just debug.log physics.gravity in the different stages to see.
@Saiansh2525 Ok so I fixed it and now I have
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GravityTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider collider)
{ Physics.gravity = new Vector3(0, 10f, 0); }
}
But the problem is that when it's activated it changes the gravity for all the boxes that I have, I only want it to change the gravityDownForce variable for the player.
@answerthegangsta I actually don't know. But after a bit of researching, I found the found this link where a person answers that question: https://answers.unity.com/questions/1021304/changing-one-gameobjects-gravity.html . If you look at the answer by NeverHopeless which is underneath the best answer I think that is the best method. They say that each Rigidbody has a property called gravityScale. it modifies the for that Rigidbody specifically. It is a float value. Replace the earlier code with Physics.gravity being assigned with GetComponent().gravityScale = gravityDownForce;. Change the variable to a float.
Your answer
Follow this Question
Related Questions
How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer
float value is reset after every update 1 Answer
How can i get the name of the gameobject that triggered a trigger? 1 Answer
Problems with triggerEnter() 0 Answers
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer