- Home /
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!
Answer by qJake · Jul 12, 2010 at 10:28 PM
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);
}
}
}
Please upvote and checkmark the answer if it worked for you.
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.
Could you make one of those in javascript form for me?
Thanks qJake. This code works perfectly. After adding the script as a component to the object, I can simply drag another object to the target
Answer by j-unity · Nov 18, 2012 at 12:07 PM
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
}
}
Answer by setn2007 · Sep 08, 2014 at 08:33 PM
I put this on my 2d enemy char and it works great let me know what you think!!!
pragma strict
/java script var player : Transform; // player var self : Transform; // object you want to face the player on x axis only
function Start () {
}
function Update () { if (player.transform.position.x <= self.transform.position.x) //players spot in world space as opposed to enemy "self" spot { self.transform.rotation = Quaternion(0,180,0,0);// flips enemy around to face the player on x axis only } else if (player.transform.position.x >= self.transform.position.x)//players spot in world space as opposed to enemy "self" spot { self.transform.rotation = Quaternion(0,0,0,0);// flips enemy around to face the player on x axis only }
}
Answer by kevinspawner · Oct 09, 2014 at 10:26 AM
You can add this below simple script which will make the object point towards camera always. Just in case if the camera rotation is messed up in your scene. Change the Vector3 .Left to other directions. Hopefully it helps!
{
public Camera targetCamera;
// Update is called once per frame
void Update()
{
transform.LookAt(transform.position + targetCamera.transform.rotation * Vector3.left,
targetCamera.transform.rotation * Vector3.up);
}
}
Answer by proVeritas_vk · Jul 02, 2018 at 07:30 AM
This helps me:
public Transform player;
...
Vector3 fwd = player.forward; fwd.y = 0; transform.rotation = Quaternion.LookRotation(fwd);
where transform is transform of object you want
Your answer
Follow this Question
Related Questions
Joystick 2D Sprite Rotation and Movement 0 Answers
Explain this script :) plz 1 Answer
Animation Must be marked as legacy 4 Answers
Ground checking with a frequently rotating player 2 Answers
Is there any way for a Sprite Renderer to use mipmaps? 1 Answer