3
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ This worked, thank you very much. :) What would be best in your opinion for multiple players: Raycast or collision? \$\endgroup\$ Jun 15, 2014 at 12:27
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – House
    Jun 15, 2014 at 13:06
  • \$\begingroup\$ 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. :) \$\endgroup\$ Jun 15, 2014 at 13:14
  • \$\begingroup\$ 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". \$\endgroup\$
    – House
    Jun 15, 2014 at 13:17
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 15, 2014 at 13:20
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .