Graphics Reference
In-Depth Information
if(currentObjectNum> totalSpawnObjects-1 )
currentObjectNum=0;
}
}
4.4 Set Gravity
Changing gravity is often frowned upon by developers because doing so changes the way
objects behave in the physics simulation. It is highly recommended that your game physics
values, game world scale, and speeds are as close to real-life numbers as possible. Making
a game with an environment scaled too high or too low can cause problems with the
collisions system as well as make your coordinate system difficult to manage. Altering
gravity to extremely high values may cause objects to behave strangely or even push down
through collision meshes. Dealing with strange physics values needs to be handled with a
little care and awareness of “entering the twilight zone” in physics behavior terms.
The reality is that having realistic gravity with realistically scaled 3D models and
realistic physics behavior may not always be either possible or, in fact, necessary. Perhaps
the effect you are going for is something outside of realistic, in which case it may become
necessary to modify physics values such as gravity. Games based on another planet would
likely be the most obvious use for changing these values. In the example games in this
topic, when gravity is altered it is purely on a personal preference of how I like game phys-
ics to feel.
The SetGravity.cs script sets gravity once in the Start() function and disables itself
to save any unnecessary overhead. Its logic is extremely simple, being a single call to set
Physics.gravity to a 3D vector and then disabling itself by setting this.enabled to false. The
variable this is simply a reference to itself, a way of telling the script to refer to itself:
public class SetGravity : MonoBehavior {
public Vector3 gravityValue = new Vector3(0,-12.81f,0);
void Start () {
Physics.gravity=gravityValue;
this.enabled=false;
}
}
4.5 Pretend Friction—Friction Simulation to Prevent
Slipping Around
The easiest way to move objects around in Unity is to simply apply forces to your rigid-
bodies. By using Rigidbody.AddForce and perhaps a multiplier based on Input.GetAxis
or something similar, it is quite easy to get objects moving around, but the only way to
have them come to a stop is either to have them make friction against something else in
the game world or to change drag values on the rigidbody. This may not always be an ideal
Search WWH ::




Custom Search