Saturday, October 29, 2022
HomeGame Developmentunity - Opponents hold reference to outdated ball as a substitute of...

unity – Opponents hold reference to outdated ball as a substitute of newly-spawned ball


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

When the ball is destroyed, in keeping with my concept, a brand new one ought to be created and opponents ought to attain for it as a substitute.

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

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments