- Home /
Help with school project, collision, destroying objects
Hello guys, I was wondering if you guys could help me out. It's the last thing that needs to happen and then I'm sure to win a prize with this thing at school!
Okay, so here is the problem. I have a first person perspective game, where people need to run through a level as fast as they can. They need to visit certain key points ("cyrstals") before the level is finished. I was going to do this by having them collide with these crystals.
I have 7 crystals through the level.
How do I make it so that it registers the collision between the object, destroys the light effect of it so that you can see you've seen it already, and register how much you've collided as well?
Answer by Kryptos · Apr 12, 2012 at 11:43 AM
Add a script to your crystal that deals with physics trigger detection. You will need to have a collider on each crystal (a BoxCollider or a SphereCollider should do the job).
Example of script:
void OnTriggerEnter( Collider other )
{
// test to make sure the player hit the trigger
// for example:
if(other.gameObject.name == "Player")
{
// call a method on the player to increment a counter
other.gameObject.GetComponent<PlayerScript>().PlusOne();
// destroy the crystal
Destroy( this.gameObject );
// or disable light
DisableLight();
// and remove collider
Destroy(GetComponent<Collider>());
}
}
Answer by fafase · Apr 12, 2012 at 12:58 PM
Almost the same in Js. Add this one to your crystal ball prefab, I would guess the lighting system is childed:
Crystal.js
static var count: int = 7;
var pling :AudioClip;
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag =="Player"){
count -=1;
AudioSource.PlayAtPoint(pling,transform.position);
Destroy(gameObject);
}
on your player or level ground or any object that will remain
function Update(){
if(Crystal.count == 0)
{
Application.LoadLevel("WinningScreen");
}
}
I guess you mean count -= 1
if you want to load a level when the count reaches 0.
Thank you very much, that did the trick. I'm going to use the C# version posted above, as the rest is in C# as well. Thanks though! :D
I am not pushing you to use $$anonymous$$e, but just in case you would not know, you actually can mix c# and js scripts. Unity detects which language is used and compile them to binary. So just for you to know that if you found a script in the other language you wouldn't need to translate it.