- Home /
Cheap gravity script
hi,
I understand that Unity's Gravity calculation for Rigidbodies is quite expensive for mobiles devices. I was wondering if there is a work-around to this, possibly a script simulating the same effect / acceleration?
Answer by $$anonymous$$ · Feb 16, 2016 at 06:25 PM
var velocity: Vector3 = Vector3(5, 5, 0);
var gravity: float = 9.8;
function Update(){
// apply gravity
velocity.y -= gravity * Time.deltaTime;
// calculate new position
transform.position += velocity * Time.deltaTime;
}
Answer by aldonaletto · May 20, 2012 at 07:30 PM
Calculating gravity effects is the cheapest part - the big problem comes from collision detection and reaction to collisions, other tasks that the management of a rigidbody requires.
If you want to simulate gravity in a simple object, just calculate its movement frame by frame:
var velocity: Vector3 = Vector3(5, 5, 0); var gravity: float = 9.8;
function Update(){ velocity.y -= gravity Time.deltaTime; // apply gravity transform.position += velocity Time.deltaTime; // calculate new position } This simple code will make the object to which it's attached to describe a ballistic trajectory. Collisions aren't detected, but maybe a good compromise could be achieved by adding a kinematic rigidbody to the object and setting its collider to trigger, then using OnTriggerEnter to detect collisions.
Your answer
Follow this Question
Related Questions
Gliding vs bad acceleration dilema 0 Answers
Rigidbody2D Grounded 4 Answers
Check if Rigidbody collider is grounded 3 Answers
Check if entire box collider has air below, instead of just a point on the box? 1 Answer
Where is the general Rigidbodies Drag? 3 Answers