Skip to main content
Clarity
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Rotate object to closedclosest clone?

I'm working on TDa tower defense game. 

I'm trying to make my gun object to rotate and shoot first closed clone object"enemy"the nearest instance of the "enemy" object. 

My problem here is my gun script rotaterotates only to the main enemy object and not at clone objectits cloned objects or even the closedclosest one !! 

I need my gun to rotate and facingface the closedclosest enemy to himitself. 

Here is my clone script:

 public GameObject enemy;               
public float spawnTime = 3.0f;            
public Transform[] spawnPoints;         

void Start ()
{
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
}

void Spawn ()
{
    int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

Gun script:

    private GameObject target;
private float enemy_distance;
 
void Start()
{
   // -------------
} // end Start

void Update()
{
    target = GameObject.FindGameObjectWithTag("enemy");
    enemy_distance = Vector3.Distance(transform.position, target.transform.position);
        if (enemy_distance <= 20) {
            Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * 2);
            transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
            }

}// end update

Rotate object to closed clone?

working on TD game. I'm trying to make gun object to rotate and shoot first closed clone object"enemy". My problem here is my gun script rotate only to main enemy object and not at clone object or even the closed one !! I need my gun rotate and facing the closed enemy to him. Here is my clone script:

 public GameObject enemy;               
public float spawnTime = 3.0f;            
public Transform[] spawnPoints;         

void Start ()
{
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
}

void Spawn ()
{
    int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

Gun script:

    private GameObject target;
private float enemy_distance;
 
void Start()
{
   // -------------
} // end Start

void Update()
{
    target = GameObject.FindGameObjectWithTag("enemy");
    enemy_distance = Vector3.Distance(transform.position, target.transform.position);
        if (enemy_distance <= 20) {
            Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * 2);
            transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
            }

}// end update

Rotate object to closest clone?

I'm working on a tower defense game. 

I'm trying to make my gun object rotate and shoot the nearest instance of the "enemy" object. 

My problem here is my gun script rotates only to the main enemy object and not at its cloned objects or even the closest one! 

I need my gun to rotate and face the closest enemy to itself. 

Here is my clone script:

public GameObject enemy;               
public float spawnTime = 3.0f;            
public Transform[] spawnPoints;         

void Start ()
{
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
}

void Spawn ()
{
    int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

Gun script:

private GameObject target;
private float enemy_distance;

void Update()
{
    target = GameObject.FindGameObjectWithTag("enemy");
    enemy_distance = Vector3.Distance(transform.position, target.transform.position);
    if (enemy_distance <= 20) {
            Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * 2);
            transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
    }

}
Source Link
Omer
  • 214
  • 5
  • 17

Rotate object to closed clone?

working on TD game. I'm trying to make gun object to rotate and shoot first closed clone object"enemy". My problem here is my gun script rotate only to main enemy object and not at clone object or even the closed one !! I need my gun rotate and facing the closed enemy to him. Here is my clone script:

 public GameObject enemy;               
public float spawnTime = 3.0f;            
public Transform[] spawnPoints;         

void Start ()
{
    InvokeRepeating ("Spawn", spawnTime, spawnTime);
}

void Spawn ()
{
    int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

Gun script:

    private GameObject target;
private float enemy_distance;

void Start()
{
   // -------------
} // end Start

void Update()
{
    target = GameObject.FindGameObjectWithTag("enemy");
    enemy_distance = Vector3.Distance(transform.position, target.transform.position);
        if (enemy_distance <= 20) {
            Quaternion rotation = Quaternion.LookRotation(target.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.fixedDeltaTime * 2);
            transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.z);
            }

}// end update