Search Unity

Code to predict where to intercept player completely breaks when moving diagonally.

Discussion in 'Navigation' started by HungryPorpoise, Jul 9, 2022.

  1. HungryPorpoise

    HungryPorpoise

    Joined:
    May 26, 2020
    Posts:
    2
    I have some code to predict exactly where the enemy should intercept the player. The code works fine when the player is moving horizontally or vertically, but when the player moves diagonally the code predicts the intercept point to be behind the player in the complete wrong direction.
    Code (CSharp):
    1. public GameObject enemy;
    2.     public GameObject player;
    3.     public GameObject moveTo;
    4.  
    5.     private Vector3 enemyPos;
    6.     public float enemySpeed;
    7.     private Vector3 playerPos;
    8.     private Vector3 playerMoveVelocity;
    9.  
    10.     public PlayerMovement pm;
    11.     public EnemyAI eai;
    12.  
    13.     private float distance;
    14.     private float pmvMagnitude;
    15.  
    16.     public Vector3 moveToPos;
    17.  
    18.     //i have no idea how the hell these work.
    19.     private float a;
    20.     private float b;
    21.     private float c;
    22.  
    23.     private float t;
    24.  
    25.     private Vector3 D;
    26.  
    27.     public void Update()
    28.     {
    29.         //the set up
    30.         enemyPos = enemy.transform.position;
    31.         playerPos = player.transform.position;
    32.         playerMoveVelocity = pm.moveVelocity;
    33.  
    34.         //the code
    35.         D = enemyPos - playerPos;
    36.         distance = D.magnitude;
    37.  
    38.         pmvMagnitude = playerMoveVelocity.magnitude;
    39.         a = Mathf.Pow(enemySpeed, 2) - Mathf.Pow(pmvMagnitude, 2);
    40.         b = 2 * Vector3.Dot(D, playerMoveVelocity);
    41.         c = -Vector3.Dot(D, D);
    42.  
    43.         if ((Mathf.Pow(b, 2) - (4 * (a * c))) < 0)
    44.         {
    45.             moveToPos = playerPos;
    46.         }
    47.  
    48.         else
    49.         {
    50.             t = (-(b) + Mathf.Sqrt(Mathf.Pow(b, 2) - (4 * (a * c)))) / (2 * a);
    51.  
    52.             moveToPos = ((t * playerMoveVelocity) + playerPos);
    53.         }
    54.         moveTo.transform.position = moveToPos;
    55.     }
    If someone could tell me what I am doing wrong and what I could do to fix it that would be great! this really stumped me...
     
  2. Tion-Gaming

    Tion-Gaming

    Joined:
    Jan 30, 2015
    Posts:
    28
    You may need to give a bit more context to the equations and logic you are using there, otherwise its just meaningless maths. Whats likley is you are just getting a sign being flipped in your code because of an edge case. You need to check if it happens in all directions or just a specific one, that will tell you what is causing your problem, the position of the enemy relative to the player also plays a factor sometimes as well as both of their positions relative to the origin. I had a similar issue for my AI when crossing over the z axis where I was making some assumptions about the sign of the vectors. Since magnitude and and squared values will always be positive your problem is probably coming form the dot product. I use the dot product to tell me if two vectors are pointing in roughly the same direction, If they are, it will be positive, but if they are pointing in opposite directions it will be negative. If they are perfectly perpendicular it is zero, quite a handy trick.