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!
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!
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);
}
}
}
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
}
}
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
}
}
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);
}
}
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