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

Look At Target - Reverse Face Addition

Discussion in 'Scripting' started by Deleted User, Nov 19, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    Simple script to look at a target, I'd like to add some sort of conditional statement ( boolean : true or false ) that will help me choose front or back facing depending on my requirements ?

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class LookAtTarget : MonoBehaviour {
    5.  
    6.     public Transform Target;
    7.  
    8.     void Update()
    9.     {
    10.         if(Target != null)
    11.         {
    12.             transform.LookAt(Target);
    13.         }
    14.     }
    15. }
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class LookAtTarget : MonoBehaviour {
    5.    
    6.     public Transform Target;
    7.     public bool ReverseFace = false;
    8.    
    9.     void Update()
    10.     {
    11.         if(Target != null)
    12.  
    13.         if (ReverseFace == false)
    14.         {
    15.             transform.LookAt(Target);
    16.         }
    17.  
    18.         if (ReverseFace == true)
    19.         {
    20.             // Reverse this ?
    21.             transform.LookAt(Target);
    22.         }
    23.     }
    24. }
     
    Last edited by a moderator: Nov 19, 2015
  2. jmjd

    jmjd

    Joined:
    Nov 14, 2012
    Posts:
    48
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtTarget : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public bool reverseFace;
    8.  
    9.     void Update()
    10.     {
    11.         if(target != null)
    12.         {
    13.             Vector3 lookAtDir = target.position - transform.position;
    14.             if(reverseFace)
    15.                 transform.forward = -lookAtDir;
    16.             else
    17.                 transform.forward = lookAtDir;
    18.         }
    19.     }
    20. }
     
  3. Deleted User

    Deleted User

    Guest

    Perfect, thanks jmjd ! :p