Search Unity

How do I move an object towards a moving object within a coroutine?

Discussion in 'Scripting' started by obmoc, Nov 21, 2017.

  1. obmoc

    obmoc

    Joined:
    Sep 24, 2017
    Posts:
    8
    Hello,

    I've been trying to figure out in a coroutine how to move a gameobject towards another gameobject that is moving, but I haven't had any luck. I can get my player to move towards a static location(where the "enemy" was) but I can't figure out how to get the current position after it moves.

    How would I go about doing this?

    Thanks!

    Code (CSharp):
    1.  
    2.         public void SetDestination(Vector3 worldPos)
    3.         {
    4.             navMeshAgent.destination = worldPos;
    5.         }
    6.  
    Code (CSharp):
    1.         private bool IsTargetInRange(GameObject target)
    2.         {
    3.             var distanceToTarget = (target.transform.position - transform.position).magnitude;
    4.             return distanceToTarget <= weaponSystem.GetCurrentWeapon().GetMaxAttackRange();
    5.         }
    Code (CSharp):
    1.         IEnumerator MoveToTarget(GameObject target)
    2.         {
    3.             print("calling MoveToTarget");
    4.          
    5.             while (!IsTargetInRange(target))
    6.             {
    7.                 character.SetDestination(target.transform.position);
    8.  
    9.                 yield return new WaitForEndOfFrame();
    10.             }
    11.             yield return new WaitForEndOfFrame();
    12.         }
     
    Last edited: Nov 26, 2017
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Assuming you have a reference to the enemy you're chasing
    Code (csharp):
    1.  
    2. var position = enemy.transform.position;
    3. player.transform.position = Vector3.MoveTowards(player.transform.position, enemy.transform.position, PlayerSpeed * Time.deltaTime);
    4.  
     
  3. obmoc

    obmoc

    Joined:
    Sep 24, 2017
    Posts:
    8
    Thanks for the reply, doesn't this need to be called within the update loop?
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You can do it in a coroutine, it just needs a loop of some kind.
    Code (csharp):
    1.  
    2. IEnumerator followPlayer()
    3. {
    4.        while(true){
    5.         float playerDistance = Vector3.Distance(transfrom.position , player.position);
    6.         if(playerDistance > 5.0f)
    7.         {
    8.             //move toward player
    9.  
    10.         }
    11.         yield return null;
    12.        }
    13. }
    14.  
    That's just a quick example, I'm sure there are better ways to do it.
     
    Last edited: Nov 25, 2017
  5. obmoc

    obmoc

    Joined:
    Sep 24, 2017
    Posts:
    8
    hmm, I am looping my current code, but I am using navmeshagent.destination(target.position). How is this different?
    I've updated my original post with my code.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It's not any different. Doesn't it work?
     
  7. obmoc

    obmoc

    Joined:
    Sep 24, 2017
    Posts:
    8
    If I run it like it is now(code posted in OP) the player will run toward the last position of the enemy(when it was clicked) The position isn't being updated.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539