- Home /
C# 2d Always Rotate Facing Gameobject
I'm trying to make a 2d scripted gameobject always face the target transform. I tried using a quaternion.slerp but it's not nearly as reactive as I want it to be and it only works in the instance the target transform is rotating. Are there other means making the 2d scripted gameobject face the target transform or is ther something I need to change in my Quaternion.Slerp?
void Seek (){
Vector3 direction = target.position - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, Time.deltaTime * 5);
if(direction.magnitude > minDistance){
Vector3 moveVector = direction.normalized * moveSpeed * Time.deltaTime;
transform.position += moveVector;
}
}
Answer by Downstream · Feb 20, 2015 at 06:44 AM
Sorry, I didn't check my reference answers. To do that, transform.LookAt also takes a plain Vector3 as the first argument, then you need to set the y-value of that vector3 to equal the height, y, of the billboard(that's what it's called), like so:
using UnityEngine;
using System.Collections;
public class Looking : MonoBehaviour {
private Vector3 pos;
GameObject player;
void Start () {
player = GameObject.Find("First Person Controller");
}
void Update () {
pos = player.transform.position;
pos.y = transform.position.y;
transform.LookAt(pos,Vector3.up);
}
}
The position holder variable, pos, needs to be in the lower scope or else you will be reallocating it every time Update runs.
Your answer
Follow this Question
Related Questions
How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers
Auto leveling help 0 Answers
Transform a gameobject y rotation to another gameobject y rotation 1 Answer
CharacterController.Move Not Corresponding to gameobject.transform.rotation 1 Answer
Flip over an object (smooth transition) 3 Answers