Search Unity

Need help with using Sphere collider as trigger.

Discussion in 'Scripting' started by zXSmashXz, May 28, 2015.

  1. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    Ok, here is the scenario.

    GameObject 1 (A tower defense style turret) has a child object which is a sphere with weapons attached to it.
    I want the sphere to rotate and look at the player when the player gets inside its sphere collider component.

    I want to use a bool to check if the player is in range of the collider, and if so have the turret to face the player and shoot. Any help on this is appreciated thanks.
     
  2. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    I am new to unity and scripting. I have looked in the unity handbook for a solution and cannot find one.
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,440
    Did you get the trigger part to work?
    as in here:
    http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers

    Then you can check if its player who entereted trigger, with:
    http://docs.unity3d.com/ScriptReference/Component.CompareTag.html *Player needs to be tagged as "Player"

    Then you can check distance,
    http://docs.unity3d.com/ScriptReference/Vector3.Distance.html

    And look towards player,
    http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
    (although later you might want to turn slowly toward player instead..)
     
  4. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    Thank you :) yea I know how to do the trigger part. Just confused how to reference it in the script, and how to use the reference for an action.
     
  5. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    Just needed someone to point me in the right direction and you have done a great job :) Thanks again