Question by
Blarghenschnarf · Aug 28, 2019 at 02:35 PM ·
targeting
transform.LookAt not looking at the target
I'm instantiating a missile prefab from a launcher that's supposed to look at the player on start and keep moving forward. Problem is, whenever a missile is instantiated, it looks at the world origin instead of the player. Here's the code:
public class ProjectileScript : MonoBehaviour
{
Transform _transform;
Rigidbody _rigidbody;
public GameObject target;
void Start()
{
_transform = GetComponent<Transform>();
_rigidbody = GetComponent<Rigidbody>();
_transform.LookAt(target.transform.position); //This line is the problem...i think
}
void Update()
{
_rigidbody.velocity = _transform.forward*2;
}
}
This is one of my first unity projects ever, so i'm sorry if i'm just doing something dumb, but could someone please offer a solution and a quick explanation as to why that solution works?
Comment
Do you get any error in the console
How is
target
set?Can you call
Debug.Log(target.transform.position);
before callingLookAt
and indicate the position you get?