Hi I’m really new to this and I’m nearing the end of a sample for mt game. All I need is some help.
What I need is someway to change level/end game based only on collision.
For example…I’d like it to be if the player collides with object 1 to take a life away, after 2 hits to start the level over, but if player collides with object 2 to add to score and of all object 2’s are gone to proceed to the next level, but i only need this to be based on collision, nothing fancy.|
Any idea?
You can use a script on each target object with an OnCollisionEnter function defined on it. Without knowing how your game is structured, it is difficult to suggest exactly what you need to do to make your desired effects happen. However, a generally useful technique to use with collisions is to send a message to the colliding object. The SendMessage function can be called on a Transform, GameObject, Collider and other common objects. This allows you to call a named function on the object’s script without knowing any further information about the object. For example, Object1’s script might look something like:-
function OnCollisionEnter(coll: Collision) {
coll.gameObject.SendMessage("Damage");
}
The Damage function on the player’s script might look like:-
function Damage() {
if (hitPoints == 1) {
hitPoints = 0;
} else {
RestartLevel();
}
}
Okay I’ll play around with it, thanks.
And as a side note, your sig reminds me of an Escape The Fate song.
Hm, sorry maybe I should be a little more clear.
Let’s say there are 5 Object 1s and 5 Object 2s and if player collides with either they should disappear on contact. If player collides (kills) 2 Object 1s the level restarts, if player collides (kills) with all (5) object 2s he should proceed to the next level.
You can set an object’s tag in the inspector and check it in the OnCollisionEnter function:-
function OnCollisionEnter(coll: Collision) {
if (coll.gameObject.tag == "Object1")
coll.gameObject.SendMessage("Damage");
else if (coll.gameObject.tag == "Object2") {
objCount++;
if (objCount == 5)
Application.LoadLevel("NextLevel");
}
}
It’s actually adapted from one of the many memorable quotes in the Watchmen film.