x


how do i make an object always face the player?

I'm looking for a way to make a 2d object (or any object) always face the player in 3d. How can I go about doing so using little system resources?

Thanks a ton!

more ▼

asked Jul 12 '10 at 10:07 PM

Yisreal gravatar image

Yisreal
115 8 9 12

@Yisreal, remember to checkmark the best answer, thanks.

Jul 13 '10 at 01:39 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

Put this on your object:

// C#
using System;
using UnityEngine;

public class LookAtTarget : MonoBehaviour
{
     public Transform target;

     void Update()
     {
          if(target != null)
          {
               transform.LookAt(target);
          }
     }
}
more ▼

answered Jul 12 '10 at 10:28 PM

qJake gravatar image

qJake
11.6k 43 78 161

Thanks works great!

Jul 13 '10 at 09:57 AM Yisreal

Please upvote and checkmark the answer if it worked for you.

Jul 13 '10 at 10:14 AM qJake

Can this be constrained to only the Y-axis? so that if my player passes the enemy in 2D , the enemy rotates only around the Y-axis to face in the player's direction.

Aug 04 '10 at 10:14 PM Vitruvius

Could you make one of those in javascript form for me?

Nov 18 '12 at 06:24 AM horrorgamer1337
(comments are locked)
10|3000 characters needed characters left

Hi! I´m new in the forum. You can try with this...If you are in a 2D Game and your player only moves f.e. in x axys and the enemy is stopped it only will face up the face lloking the player. I add a damp variable so make the rotation softer. I hope you can use it!!

// Script to look at camera

var targetPosition :Transform; // we have to add in the Inspector our target
var damp: int = 5; // we can change the slerp velocity here

function Update ()
{
    if ( targetPosition ) // we get sure the target is here
    {
       var rotationAngle = Quaternion.LookRotation ( targetPosition.position - transform.position); // we get the angle has to be rotated
       transform.rotation = Quaternion.Slerp ( transform.rotation, rotationAngle, Time.deltaTime * damp); // we rotate the rotationAngle 
    }
}
more ▼

answered Nov 18 '12 at 12:07 PM

j-unity gravatar image

j-unity
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5058
x2156
x1033
x212
x58

asked: Jul 12 '10 at 10:07 PM

Seen: 4225 times

Last Updated: Nov 18 '12 at 12:07 PM