Search Unity

Auto lock on target

Discussion in 'General Discussion' started by IKilledKenny_2, Apr 19, 2014.

  1. IKilledKenny_2

    IKilledKenny_2

    Joined:
    Aug 20, 2013
    Posts:
    98
    Hello Unity3D i have a question about auto lock on target?Is there any tutorials on how to make my character auto lock on another character?for example im making a 3D fighting game and i want the characters to auto lock on the other characters so that when im fighting i dont have to manually lock on the opponent,its automatically targeted on that opponent?Does anyone know any tutorials on auto targeting or know codes for it on a website perhaps?
     
  2. galika1080

    galika1080

    Joined:
    May 31, 2014
    Posts:
    1
    I think the best way to do that would be with transform.LookAt(). Create a variable for your target, and tell your camera to look at it when you are in range and a button is held down. Here is some sample code:

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var distance;
    4.  
    5. function Update () {
    6. distance = Vector3.Distance(transform.position, target.transform.position);
    7. if(Input.GetMouseButton(1)) {
    8.     if(distance < 30) {
    9.         transform.LookAt(target);
    10.         }
    11.     }
    12. }
    13.  
    Hope it works, good luck!