Search Unity

Circle trigger-area that follows the player (Best Method?). [Solved]

Discussion in 'Scripting' started by Troas, Aug 12, 2014.

  1. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I have been thinking of this issue for a while. What I've been looking for is a way to create this trigger around the player that follows him/her (obviously easy way is to parent it, this I know).

    What I do not know is how to make such an object in Unity. I can easily create an empty game object but it's in the shape of a box not a circle, and I would much rather avoid having to use a sphere because of what I want this trigger do. Having a sphere would create some problems for me.

    Is there some sort of way to create maybe an 8/10-sided object in unity, have it with no material (so it does not appear in game) and make it so that when my mouse is inside the trigger it does something different than outside the trigger?

    Honestly I am not looking for any script, I am wondering if this is the best method or if I am making it over-complicated and there is a simpler way to do it. (Such as detecting on a plane how far the mouse from the player, and if it is a certain distance from the player it can change what a left click does).

    I would like to figure out the scripting myself. Pseudo-code is fine as long as you do not enter parameters for me and let me figure it out by using API.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you can't use a combination of box colliders for this...

    You need two things, a Mesh and a MeshCollider. The easiest thing is to import a 3D mesh model and attach a MeshCollider to it. If you want to generate the Mesh on the fly, then get to learn the Mesh class and how to wrap triangles!

    I've never used it, but you can probably use OnMouseOver() or something similar to know if the mouse is over your collider.
     
    Troas likes this.
  3. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Ah ok I was thinking I was just going to have to make a custom mesh. Where would I find some kind of tutorial for making an invisible mesh? (I guess that's one that technically does not render).
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Just make sure there is no MeshRenderer on the GameObject and nothing is ever visible.

    You can still have a MeshFilter on the GameObject, with a MeshCollider, but nothing is getting rendered. The Mesh is used for the physics only.
     
    Troas and angrypenguin like this.
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yep. Use a mesh collider of whatever shape you want, though preferably one that's convex and almost certainly one with less than 255 verts (or faces? I can't remember). As Garth says, you don't need a mesh renderer. What you do need is a mesh collider and a Rigidbody. Set the collider to be a trigger, set the Rigidbody to be kinematic and ignore gravity.

    You can make the mesh you need in something like Blender pretty easily, by the way. You can probably just spawn a default cylinder with the right parameters and export it as-is, in fact.
     
    GarthSmith and Troas like this.
  6. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157

    Thanks a lot for a push in the right direction guys. I feel like meshes being used for triggers are something that not a lot of tutorials touch up on even though I see a lot of practical application for it in nearly every game type. Maybe that's just me not searching the right places.

    Y'all cleared up a lot though!
     
    PKZ_, angrypenguin and GarthSmith like this.