- Home /
Exit on Collision
HI guys, I'm very new to unity and the only programming language i know is Java. I have an assignment to make a small interactive game using any program I want and so i chose unity. Now i am making an FirstPerson game similar to slenderman (but instead of finding 8 pages i have to find 1 fire). Graphichs and movements are working correctly, now all i need is an exit and to make the play not pass in trees. and you help me please. Thank you :)
PS: Explain in detail please as I have scouted the forums for answers and still have not managed to do this. Thx again
#pragma strict
function Start () {
}
function OnCollisionEnter (Hit: Collision){
if(Hit.gameObject.tag == "FirstPersonControl"){
print("Worked");
Application.Quit();
}
}
function Update () {
}
The main problem i have is what is the coding for when my Player collides with fire to trigger the end of game.
As I pointed out earlier, tags are strings, so unless FirstPersonControl
is a string variable holding the actual tag you want to use, use "FirstPersonControl"
like this. Also, you want to put this script on the fire object, because the player can't collide with itself. Also, you need to use #pragma strict
, and not pragma strict
. Then if you put this script on your fire object, it should work.
Note: JavaScript's name doesn't mean that JavaScript has anything to do with Java. If you use Java and worked with statically typed code, you should find C# more familiar than JavaScript.
Answer by Piflik · Jan 07, 2013 at 11:49 AM
For the trees you need a collider component. Depending in how you created the trees in the first place, the probably best solution would be to add Capsule Colliders to each tree prefab.
It is not clear to me, what you mean with exit...do you want to quit the game (Application.Quit()) or load a new/the same level (Application.LoadLevel()) or do something completely different?
PS: If you want detailed answers, you need to post detailed questions.
According to the **docs**, trees can only have Capsule Collider. So you attach a capsule collider to your model in the scene view, scale it to your tree as you see fit, and save that out to a prefab. Then feed that prefab to the Terrain tool's tree generator, like the page in the link shows.
If he has his own tree, he can use whatever collider he wants. I'd still recommend a caspule, since that gives him the most desired result from a gameplay perspective.
the tree is part of the trerrain. how do i add a capsule collider? And i have creat tree colliders checked
I only tried it once to help a friend, and only used the standard tree that comes with the terrain assets, but there I just selected the prefab in the prohect folder and added a capsule collider. I placed an instance of that prefab in the scene (a real gameobject, not as part of the terrain) to position the collider correctly an then applied the changes to the prefab. After that all trees on the terrain also had colliders.
Answer by Fifra · Jan 07, 2013 at 01:09 PM
And yes i want the game to quit i have tried the code application.quit() but its not working, i did a print working before it during testing and that is not working as well. the code i'm trying is:
pragma strict
function Start () {
}
function OnCollisionEnter (Hit: Collision){
if(Hit.gameObject.tag == FirstPersonControl){
print("Worked");
Application.Quit();
} }
function Update () {
}
and i have attached it to the cylinder of the first person controller
Application.Quit()
works, except when running in the editor or in a web player. Build your game and test it as a standalone.
can't build as OnCollisionEnter is giving me errors: Script error: OnCollisionEnter
Ah, I see.. tags are strings, that's why you have compile error. Use (Hit.gameObject.tag == "FirstPersonControl")
ins$$anonymous$$d. Also keep in $$anonymous$$d that tags are not object names. By the way, why are you attaching this script to the first person controller?
The error has now changed to: The referenced script on the Behavior is missing!
where sould i attach it if not the person? with the obejct?
and the old one is showing again: Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.
Your answer
Follow this Question
Related Questions
Tree Colliders are not Triggers 1 Answer
Tree collision 1 Answer
OnTriggerExit won't detect player? 1 Answer
Tree collision 1 Answer
My trees have their collider misplaced 0 Answers