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

Locking script not locking properly

Discussion in 'Scripting' started by Lexo18, Apr 16, 2015.

  1. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    The purpose of this script is to lock onto GameObjects with the tag of Targetable and to target the closest one.

    Problems:
    It only targets the one closest at the start of the scene rather than constantly checking.
    It doesn't properly face the target rotating in weird ways.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Closestlock : MonoBehaviour {
    5.     //Holds all GameObjects in the scene with the tag of Targetable.
    6.     public GameObject [] targetable;
    7.     //The closest GameObject
    8.     public GameObject targeted;
    9.     // Use this for initialization
    10.     // Used to calculate which GameObject is closer.
    11.     float minDist;
    12.     public float dist = 0;
    13.     // how quickly you want to be able to look at the target.
    14.     public float speed;
    15.     void Start () {
    16.         //Fills the array with GameObjects
    17.         //uncomment this line if you do not need to check if new objects are being instantiated
    18.         //targetable = GameObject.FindWithTag("Targetable");
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.            
    24.         //Fills the array with GameObjects
    25.         targetable = GameObject.FindGameObjectsWithTag("Targetable");
    26.         //Runs through each GameObject
    27.         foreach(GameObject target in targetable){
    28.             //Stores the shortest distance and sets "targeted" to the shortest distance.
    29.             dist = Vector3.Distance(transform.position, target.transform.position);
    30.             //Checks to see which is closer.
    31.             if(minDist == 0){
    32.                 targeted = target;
    33.                 minDist = dist;
    34.                 }
    35.             if(dist < minDist){
    36.                 minDist = dist;
    37.                 targeted = target;
    38.             }
    39.         }
    40.         // lock on target
    41.         Quaternion targetRotation = Quaternion.LookRotation(targeted.transform.position);  
    42.         // Smoothly rotate towards the target point.
    43.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    44.    
    45.     }
    46. }
    47.  
    48.  
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Maybe you should try using transform.LookAt?
     
  3. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    I need it to smoothly move in between which as far as i know will snaps between rotations