Search Unity

Flash hitTest in Unity

Discussion in 'Scripting' started by Perditus, Aug 31, 2009.

  1. Perditus

    Perditus

    Joined:
    Aug 31, 2009
    Posts:
    7
    In flash you would use this to change scenes when the player instance hits what the script's on.

    if(this.hitTest(_root.player)) {
    _root.gotoAndStop(nextScene);
    }

    What's the Unity Javascript equivalent of this?
    (The player would be hitting a sphere collider.)

    If at all possible, if someone's able to make an example of this for me to look at, I'd greatly appreciate it.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I've attached an example project to illustrate how to do this. The main difference compared to Flash is that Unity works in 3D - the point you click on the screen doesn't correspond to a single point in the 3D world, but rather a line. The Camera class has a method called ScreenPointToRay which allows you to generate this line in the form of a Ray object. The Physics class then has a Raycast method that detects objects that intersect with the line. Note that a collider needs to be attached to the object in order for this to work. Your SphereCollider should be fine.
     

    Attached Files:

  3. Perditus

    Perditus

    Joined:
    Aug 31, 2009
    Posts:
    7
    Thank you for the help Andeeee but a hitTest in flash doesn't involve clicking the screen, say you have an object you're moving around the screen by controlling it with the arrow keys, you have another object on the screen that's sitting there, when the object you control collides, or hits, the other object it would activate the script, in my case it would go to the next scene.

    In unity you would use "Application.LoadLevel("sceneName");" rather than "_root.gotoAndStop(nextScene);", if that's the part that got you confused.

    So when the player that I'm controlling runs into the sphere collider i need it to go to the scene name.
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Oops! I actually got the Application.LoadLevel part - my brain is perfectly capable of getting confused all by itself!

    OK, if you look in the Unity inspector panel, you'll see that the SphereCollider component has a checkbox saying IsTrigger. If this is enabled, it means that the collider will be used to detect other objects passing into it rather than bouncing them away physically. A script attached to the object with the sphere collider can declare methods called OnTriggerEnter, OnTriggerExit and OnTriggerStay (check the MonoBehaviour docs for more information). These methods let you access the trigger collider from your script. You would probably just need to put the Application.LoadLevel inside the OnTriggerEnter function to do what you described.
     
  5. Perditus

    Perditus

    Joined:
    Aug 31, 2009
    Posts:
    7
    Thank you Andeeee, i got it to work, the game wouldn't work right without it.