- Home /
Homing missle travel straight if it has no target
Im creating an object that homes in on enemies once instantiated, but whenever this object doesnt have a target it just sits there, not going anywhere. I used brackeys tutorial for a 2d homing missle. Any help is welcome:
void Start()
{
target = GameObject.FindGameObjectWithTag("Enemy").transform;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector2 direction = (Vector2)target.position - rb.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.right).z;
rb.angularVelocity = -rotateAmount * rotateSpeed;
rb.velocity = transform.right * speed;
}
Comment