x


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?

more ▼

asked May 20 '12 at 07:15 PM

asimov gravatar image

asimov
157 20 37 40

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 20 '12 at 07:30 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1864
x481
x99

asked: May 20 '12 at 07:15 PM

Seen: 1464 times

Last Updated: May 20 '12 at 07:35 PM