Search Unity

I need hep with directions of navigating

Discussion in 'Navigation' started by MikeyJY, Mar 10, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Attacking offest
    upload_2020-3-10_21-9-24.png

    I' using this:
    dirToPlayer = transform.position - Player.transform.position;
    newPos = transform.position + dirToPlayer;

    But this causes the enemy to go through the player(they have the same position).
    I tried to do the offset like this:
    newPos = transform.position - dirToPlayer + new Vector3(2, 0, 2);

    But if facing another direction, the enemy goes behind the player
    If facing north I should + new Vector3(2, 0, 2); If facing south - new Vector3(2, 0, 2);
    I don't know how to explain exactly what I mean but I wish you understand.
     
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi,

    You want to walk toward Player but not cross bubble? or just stay outside the bubble?
    You can just reduce vector

    Code (CSharp):
    1. newPos = transform.position + dirToPlayer - offsetScalar*(dirToPlayer.Normalize)
    Or, you need to calculate tangent (line that touches the circle, but not crossing). Tangent can be on left or right side of the circle

    Code (CSharp):
    1. public static bool Circle2(Vector2 center, float r, Vector2 p, ref Vector2 tanPosA, ref Vector2 tanPosB)
    2.         {
    3.             p -= center;
    4.             float P = p.magnitude;
    5.             // if p is inside the circle, there ain't no tangents.
    6.             if (P <= r)
    7.             {
    8.                 return false;
    9.             }
    10.             float a = r * r / P;
    11.             float q = r * (float)Mathf.Sqrt((P * P) - (r * r)) / P;
    12.             Vector2 pN = p / P;
    13.             Vector2 pNP = new Vector2(-pN.y, pN.x);
    14.             Vector2 va = pN * a;
    15.             tanPosA = va + pNP * q;
    16.             tanPosB = va - pNP * q;
    17.             tanPosA += center;
    18.             tanPosB += center;
    19.  
    20.             return true;
    21.         }
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Thanks a lot and sorry for late answer. I have another question related. If I use agent.SetDestination, the enemy, will find the shortest path to the point that's a line. How to make an navmeshagent to rotate around the player.
    I never receive a reply on Navigation topic because It is very hard to set tags on a threads on unity forum
     
  4. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Hi,

    Agent by default rotates game objects in path it follows, so you need to disable agent rotation
    Code (CSharp):
    1. NavMeshAgent.updateRotation = false
    You can create smooth rotation from path to player object. (pseudo code, just an idea)
    Code (CSharp):
    1. update(){
    2. var fractionOfJourney = dirToPlayer-(newPos - transform.position).magnitudeSqr / dirToPlayer.magnitudeSqr
    3. var lookAt = Vector3.Lerp(newPos, Player.transform.position, fractionOfJourney);
    4. transform.rotation = Quantarion.LookRotation(lookAt);
    5. }
     
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I implemented it in csharp and it is working, thanks!
     
  6. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    355
    Your welcome!