Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to detect if an object is nearby to perform an action

Discussion in 'Scripting' started by iRyan, Nov 9, 2015.

  1. iRyan

    iRyan

    Joined:
    Jan 16, 2013
    Posts:
    20
    I am creating a demo walkthrough of an environment and I need the player to look at certain points of interest along the journey.

    I plan on using the transform.lookat to allow the player to look at the points of interest but I do not know how to code the part where the program will detect if a suitable point of interest is nearby or not.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    you can make somethink with Vector3.Distance
    or use trigger areas around your points of interest where your target is set than to your point of interest
     
  3. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
    You could give the points of interest a large collider box and set it to is trigger then if you enter collider it triggers ontriggerenter which makes your character do a lookat then make lookat null ontriggerexit. Other one is vector3.distance. You can do a search on basic enemy ai and find examples set up for this. Basically creating a script on player with transform variables and drag and drop points of interest onto it and call Vector3.Distance so when its less then value you lookat and when its not its null and reset view.
     
  4. Mr_Teels

    Mr_Teels

    Joined:
    Jul 14, 2015
    Posts:
    14
    You can put Collider into the Scenes. There are actions do detect if a Objekt is inside oder enter a collider.

    official Video Tutorial