Home Game Development unity – Opponents maintain reference to outdated ball as an alternative of newly-spawned ball

unity – Opponents maintain reference to outdated ball as an alternative of newly-spawned ball

0
unity – Opponents maintain reference to outdated ball as an alternative of newly-spawned ball

[ad_1]

I’ve the place of a ball that the opponents get and attain for it.

When the ball is destroyed, in accordance with my thought, a brand new one ought to be created and opponents ought to attain for it as an alternative.

The ball is created, however the opponents seek advice from the outdated ball. How can I make the opponents attain for the brand new balls too?

public class EnemyPoint : MonoBehaviour {
     public float pace = 5;
     Transform ball;
     public float stoppingDistance;
     public float pressure = 5000f;
     public GameObject keepHolding;
     public GameObject myEnemy;
 
    
     non-public float spawnRangeX = 10;
     non-public float spawnZMin = 15; 
     non-public float spawnZMax = 25;
     // Start is named earlier than the primary body replace
     void Start()
     {
         ball = GameObject.FindGameObjectWithTag("Ball").remodel;
 
         Instantiate(ball.remodel, GenerateSpawnPosition(), ball.remodel.rotation);
         
     }
 
     // Update is named as soon as per body
     void MountedUpdate()
     {
         EnemyWalk();
     }
 
     non-public void OnTriggerEnter(Collider different)
     {
         Vector3 dir = ball.place - remodel.place;
        
         if (ball)
         {
             
             different.GetComponent<Rigidbody>().velocity = dir.normalized * pressure * Time.deltaTime ;
     
         }
        
     }
 
     void EnemyWalk()
     {
         float rangeX = Mathf.Clamp(remodel.place.x, -50, 65);
         float rangeY = Mathf.Clamp(remodel.place.y, 5, 5);
         float rangeZ = Mathf.Clamp(remodel.place.z, -55, -55);
 
         myEnemy.remodel.place = new Vector3(rangeX, rangeY, rangeZ);
 
         if (Vector3.Distance(keepHolding.remodel.place, ball.place) < stoppingDistance)
         {
             myEnemy.remodel.place = Vector3.MoveTowards(myEnemy.remodel.place, ball.place, pace * Time.deltaTime);
         }
 
     }
     Vector3 GenerateSpawnPosition()
     {   
         float xPos = Random.Range(-spawnRangeX, spawnRangeX);
         float zPos = Random.Range(spawnZMin, spawnZMax);
         return new Vector3(xPos, 3, zPos);
     }
 }

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here