How do I complete homing missile movement?
Dear everyone
I'm making shooting game. The Homing missile will do like this
GetComponet or FindGameObjectWithTag for Players, Enemies
Set the coordinate them
Check distances between Player and Enemies.
Let the shortest distance fire. 4-1. if the Enemy is there ---> Take it. 4-2. if the Enemy is disappeared ---> Just go straight
Here 4-2, when I push fire button several times and the Targeting Enemy is taken by first missile, and the next other Homing missiles have just stopped!!
I want to solve the 4-2 section over list.
That is, the problem on b under Figures.
a. Several times missiles are pushed b. After Enemy destroyed, then other left missiles just there!! ---> Help me, please!!
plus I attached the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHMissile_2 : MonoBehaviour
{
float rotSpeed = 180f;
Vector3 dir = new Vector3();
public float missileSpeed;
private GameObject hazardBeingMeasured;
public Vector3 measuredDistance;
private GameObject closestTarget;
private Vector3 distanceDifference;
private Vector3 playerPosition;
private float currentDistance;
private float oldDistance;
private GameObject[] hazardArray;
void Start()
{
oldDistance = Mathf.Infinity;
playerPosition = GameObject.FindGameObjectWithTag("Player-1").transform.position;
hazardArray = GameObject.FindGameObjectsWithTag("Enemy");
for (int i = 0; i < hazardArray.Length; i++)
{
distanceDifference = hazardArray [1].transform.position - playerPosition;
currentDistance = distanceDifference.sqrMagnitude;
hazardBeingMeasured = hazardArray [1].gameObject;
measuredDistance = hazardBeingMeasured.transform.position - playerPosition;
if (currentDistance < oldDistance && hazardArray [i].GetComponent<Istargeted> ().isTargeted == false && playerPosition.z < hazardArray [i].transform.position.z - 5)
if (currentDistance < oldDistance)
{
closestTarget = hazardArray [i];
oldDistance = currentDistance;
}
}
// Update is called once per frame
void FixedUpdate ()
{
if (closestTarget != null)
{
dir = closestTarget.transform.position - this.transform.position;
dir.Normalize ();
float zAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;
Quaternion desiredRot = Quaternion.Euler (0, 0, zAngle);
transform.rotation = Quaternion.RotateTowards (transform.rotation, desiredRot, rotSpeed * Time.deltaTime);
Vector3 pos = transform.position;
Vector3 velocity = new Vector3 (0, missileSpeed * Time.deltaTime, 0);
pos += transform.rotation * velocity;
transform.position = pos;
}
if (closestTarget == null)
{
Vector3 pos = transform.position;
Vector3 velocity = Vector3.forward * missileSpeed;
pos += 1 * velocity;
transform.position = pos;
}
}
}
if (closestTarget != null)
if (closestTarget == null)
So, this way, Isn't it enough?
I want to see more another ways, please.