- Home /
Custom Gravity issues
I am attempting to apply custom gravity to all objects in my game. to start with all the objects should have standard (0,9,0) gravity, but as the game goes on the player has the ability to set a new 'Down' direction for themselves or any item/other player
so far I have
public class GravityScript : MonoBehaviour { public Vector3 GravityForce; public Vector3 tempGrav; Vector3 newUp; Vector3 CoG; // Use this for initialization void Start () { this.rigidbody.useGravity = false; GravityForce = Physics.gravity; CoG= this.transform.position+this.transform.up; Vector3 newUp = this.transform.up; } // Update is called once per frame void Update () { CoG= this.transform.position+this.transform.up; if(Input.GetButtonDown("Fire1")) { RaycastHit hit=new RaycastHit(); Ray ray = new Ray(CoG, Camera.main.transform.forward); if(Physics.Raycast(ray, out hit)) { tempGrav = hit.normal *-10; // tempGrav = tGrav; newUp = hit.normal; } } if(tempGrav!=GravityForce&&Input.GetButtonUp("Fire1")) { GravityForce = tempGrav; } } void FixedUpdate() { rigidbody.AddForce( GravityForce * Time.fixedDeltaTime, ForceMode.Acceleration); } }
it is not a simple case of inverting gravity, as it could be assigned to an angular surface. The script above only applies for modifying there own gravity, well it should I cannot seem to get it to work.
any ideas - I know I have to implement selfrighting as well - pointers on that would be good too!
Thanks
Answer by Fornoreason1000 · Mar 23, 2014 at 11:14 AM
how does your custom gravity work? are you using rigigbodies or id you make a script?
if script you can make a child script from it, then give a public function that changes the direction of gravity.
if rigid bodies you could turnoff gravity and rotate the gravity vector (Physics.Gravity, store it in another variable!) to the direction the player sets it.
This code serves as a guide, copying paste pasting it will probably cause all sorts of weird things(e.g character slowly being pulled through the floor, jitterbugging, poor collisions)
Vector3 grav
public void ApplyMyGravity() {
//apply manual gravity
transform.position =- grav * Time.Deltatime
}
public void SetMyGrav(Vector3 dir) {
grav = Physics.gravity * Quaternion.Euler(dir);
}
Hope it helps
I wanted a script that I could attach to any object (including FPS player) so all gravity for that item, falling etc is based on the new vector. I managed to get it working a bit but I had to put the multiplier up to -100. Which isn't right.
please post comments as comments
the code as i said serves as a guide, I can't write scripts for you, i can only tell you how to write them yourself and to guide you when you get stuck writing to get you going again.
as for the multiplier. Its sounds like its conflicted with other movements in another script probably your FPS Player
this code actually goes inside FPS player, you know where all the physic logic is...if you don't it will just conflict each other causing glitches.
Your answer
