I tried for a homing missile and my script working fine only if both of the game objects (target and missile) having position.z =6. When I change both z values to 0 my script goes nuts. Can someone please explain me what is going on here ? It seems the transform.LookAt() is not working if the tragetpos is having z =0. Please note that my project is setup for 2D
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Rigidbody2D target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 tragetpos = target.transform.position;
transform.LookAt(tragetpos);
transform.Translate(transform.position * Time.deltaTime * Globals.homingmissile_speed_factor);
transform.rotation = Quaternion.identity;
}
}
I noticed you have a 2D rigidbody, so, I am guessing your game is 2D, so, why change to z pos on any transform?
Also, yes, what is happening is the Transfrom looking at, is turning away from the camera. If you click on it, while this is happening, watch the Transfrom. You’ll see its a rotational issue.
I ran into this problem a while ago, one thing I did to help was place two sprites in the Transfrom, one normal, the other rotated 90 on the y axis I beleive.
Hi,
transform.LookAt() is simple function and wrote correctly in your code (for this pice of code). But the next line looks a litttle bit strange.
I mean this line: