Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm struggling to figure this code out by myself. I'm able to check if the objects are in the scene, and if they are print a message. I now want the GameObject of tag Zone to display a message to let me know when I've walked on it or at very least, touched it.

Am I entirely wrong with trying to do this with Raycast, or would I be better off trying to figure out how to do this with purely Collisions or something? Just looking for advice more than anything, I really want to figure this out as best I can.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

One way to do this is to use a collider (sphere, cube, cylinder, etc.). Then make the collider a trigger collider and add a rigid body.

Along with a collider attached to your Zone gameobject, you can attach a script with something like the following:

void OnTriggerEnter(Collider other) {
    Debug.Log("Zone triggered!");
}

The OnTriggerEnter method will fire once another collider has touched the trigger collider.

share|improve this answer
    
This worked, thank you very much. :) What would be best in your opinion for multiple players: Raycast or collision? –  Jack Miller Jun 15 at 12:27
1  
@JackMiller I may go with either, like I say in the raycast answer, with that approach you can keep all the code in the player. But you can get the game object from the collider in the above script and get any other component from that, allowing you to get a component that distinguishes between players. So, either one works, it's just a style thing. –  Byte56 Jun 15 at 13:06
    
This is the code I created, going by what you say it should be fine. //Enter Zone void OnTriggerEnter (Collider col){ if (col.gameObject.name == "Player") { print ("You are in the zone!"); inZone = true; } } Thank you again for your assistance. :) –  Jack Miller Jun 15 at 13:14
    
If you're using that in a network situation, other players can trigger it (for the local player) , if the local game objects representing them are also named "Player". –  Byte56 Jun 15 at 13:17
    
Yes I understand that. I figured that I'd create a couple more tags and extend the scripting to be like teamA and B players. I am now coming to the conclusion that each player will multiply the points given and force their team to win. Not entirely a bad thing though. –  Jack Miller Jun 15 at 13:20

This can be done with raycasting. Cast a short ray (using the distance parameter) downwards from your walking object. Make the walked on object part of a special layer for trigger objects. When casting your ray, you can restrict it to only collide with that layer of objects.

This strategy allows you to keep all the code for checking when you walk over an object inside the object doing the walking.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.