A* Pathfinding (E09: path smoothing 2/2)

In this episode we finish implementing path smoothing. Paths are also made to update when the target moves, and units made to decelerate towards the end of their paths.
Source code: github.com/SebLague/Pathfinding
If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) through Patreon: bit.ly/sebPatreon
Or a once-off donation through PayPal:
bit.ly/SupportGamedevTutorials

Пікірлер: 48

  • @ThaneKrios63
    @ThaneKrios636 жыл бұрын

    I might be wrong, but there seems to be a problem with the way seeker moves, It's approaching the Y coordinate of lookPoint worldposition. However in 2D it doesn't matter since we have only 2 axis;

  • @alvarobasoalto889
    @alvarobasoalto8897 жыл бұрын

    Thanks a lot Sebastian, you don't know how I appreciate your tutorials and help. I have a little problem with this last two episodes of the tutorial because in my app I have my main Camera view controlled by the mouse, so it change the lookrotation and create a crash making the camera go crazy and lose the control of it and the player don't make any useful movement. Sorry if I don't explain it very well, I don't know how else can I explain it.

  • @kuteninja
    @kuteninja7 жыл бұрын

    Why do you prefer to do Lerp on rotations instead of the LookAt or RotateTowards functions?

  • @hemanthbs2649
    @hemanthbs26495 жыл бұрын

    hi sebastian, how do we rubber band the path. (eliminate nodes that are not required)

  • @mike_o7874
    @mike_o78747 жыл бұрын

    i did something like is that any good? or does the Add funtion every frame is harsh on cpu // current pos to current way point if (dist targetIndex++; if (targetIndex >= path.Length) { if (dist yield break; } currentWaypoint = path[targetIndex]; temp = -((Vector2)this.realMe.position - currentWaypoint).normalized; inc = 0; } then to turn "smothly" if (inc { inc += 0.005f; t = t * (1 - inc) + temp * inc; } cc.Movement = t;

  • @jegersma
    @jegersma6 жыл бұрын

    the smoothing makes the units walk through the unwalkable areas, what is going on?

  • @Electorch

    @Electorch

    Жыл бұрын

    kzread.info/dash/bejne/lqJlurSCkbW1fZc.html the SimplifyPath method is the culprit. You could do this instead to simplify it Vector3[] SimplifyPath(List path) { List waypoints = new List(); for (int i = 1; i { bool simplifiedBool = Random.Range(0, 2) if (simplifiedBool) { waypoints.Add(path[i].worldPosition); } } return waypoints.ToArray(); }

  • @Neran280

    @Neran280

    Жыл бұрын

    @@Electorch Im not sure whether the simplification is really the culprit. The problem is really the smoothing, because it tells the unit that it should move to the next waypoint when the unit moves beyond a line. The offset of this line to its corresponding point is determined by the turn distance. If the line lies much before the waypoint then the unit starts targetting the next waypoint earlier. This is why it moves through unwalkable cells. Increasing the number of waypoints does not change this, because the corresponding waypoint that makes the unit move through the unwalkable cells is still on the same position. Edit: Also the turnspeed/speed ratio affects whether a unit may move through obstacles. If the turn speed is too low then the unit rotates slower into the direction of the next waypoint.So the unit follows the old direction for a longer time period. This may be a path that does not lie on the computed (optimal) path and could be unwalkable cells.

  • @samueltawas2912
    @samueltawas29122 жыл бұрын

    hi Mr. @SebastianLague can you help me figure out formula to find pole or gradient line on vector 3D? I tried modified your 2D A* into 3d A*. Because of practice implement floating object such as "Fish Chasing on Its Target".

  • @Brabec95
    @Brabec957 жыл бұрын

    Sebastian, I really appreciate your works, you are amazing. I still cannot follow your math skills on Landmass gen : mesh though.

  • @paradisumworldparadisumwor921
    @paradisumworldparadisumwor9212 жыл бұрын

    How can I make the grid work for 3d scenes with more than 1 floor that can have multiple connections between floors?

  • @lofisenpai2872
    @lofisenpai28727 ай бұрын

    This project is great and I learned a lot following it. However, has anyone implemented this dynamically at runtime? I cant get it to work.

  • @samueltawas2912
    @samueltawas29122 жыл бұрын

    anyone know how to make Line class supports for 3D Vector? Working on project fish (float obj) chasing its target

  • @sdolby1
    @sdolby17 жыл бұрын

    Hi, I've noticed a small problem, not sure if it because of my implementation of not. Because of the smooth curves the seekers seem to be able to go through walls.

  • @logan_gameDev

    @logan_gameDev

    7 жыл бұрын

    ^^ this

  • @logan_gameDev

    @logan_gameDev

    7 жыл бұрын

    turning down the turnDst somewhat worked for me.

  • @GrashalmTuts

    @GrashalmTuts

    7 жыл бұрын

    Stephen Dolby yeah, have that problem too

  • @Od1UmScourge

    @Od1UmScourge

    6 жыл бұрын

    hey! any one know how fix it?

  • @JustAPersonalUseBarb

    @JustAPersonalUseBarb

    3 жыл бұрын

    Too late for you but others may appreciate an answer still. Two main things. 1: Make the obstacleProximityPenalty super high, like 50 or something if grass is just 5. This makes the dude steer clear. 2. Add a physics body to the obstacles and make them kinematic so they don't move. This'll make the seeker unable to move through them and they'll just run into them until they rotate enough. You could also make your seeker a physics body too but that'd take more work.

  • @Electorch
    @Electorch Жыл бұрын

    3:06 it should be speed * Time.deltaTime * Vector3.forward because order of multiplication (Vector3.x * time)(Vector3.y * time)(Vector3.z* time) (Vector3.x * speed)(Vector3.y * speed)(Vector3.z* speed) = 6 multiplication instead of ((speed*time)*Vector3.x)((speed*time)*Vector3.y)((speed*time)*Vector3.z) = 4 mutliplication

  • @marioluigi2995
    @marioluigi29957 жыл бұрын

    Sebastian, although I have watched some videos of this serie. I haven't tried to implement your solution yet because I'm not sure if this pathfinder is dynamic or not. Is this solution fits for dynamic cases? Can you make a video adapting it for dynamic if it doesn't support it yet? Great vids!

  • @Atomic-Potato
    @Atomic-Potato2 ай бұрын

    if anyone wants to do the same but without rotating the character, you just do the following, in which you just lerp between the current direction and the target direction and add the resulting vector to the transform position. (I'm doing this in 2D for my case, but its pretty much the same for 3D) Vector2 moveDirection = Vector2.zero; while (isFollowingPath) { // ... if (isFollowingPath) { Vector3 targetDirection = (_smoothPath.WayPoints[_pathIndex] - (Vector2)transform.position).normalized; moveDirection = Vector2.Lerp(moveDirection, targetDirection, Time.deltaTime * _smoothPathTurningSpeed).normalized; transform.position += (Vector3)moveDirection * Speed * Time.deltaTime; }

  • @linkincnt
    @linkincnt5 жыл бұрын

    What generates so much GC allocation on your code? I can´t seen to figure it out.

  • @kyledavison8968

    @kyledavison8968

    3 жыл бұрын

    Way late here but it'll be because of all the variable creation in the loops, create the vars outside the loops and change the values instead. Uses much less memory

  • @saniel2748

    @saniel2748

    3 жыл бұрын

    @@kyledavison8968 Not entirely true. Vectors and quaternions do not generate garbage, while classes and especially arrays do. I would recommend rewriting whole thing using NativeArrays

  • @MrSpherical_jr
    @MrSpherical_jr2 жыл бұрын

    how to visible the navigation line on game screen

  • @etopowertwon
    @etopowertwon3 жыл бұрын

    It'll also be nice also to see implementation of something more complex like NavMesh (not builtin, but built from scratch) or Theta*

  • @oliverdowning1543
    @oliverdowning15434 жыл бұрын

    You said -1 but you meant +1 for the previous waypoint as the variable is decreasing not increasing

  • @JustAPersonalUseBarb
    @JustAPersonalUseBarb3 жыл бұрын

    In case someone stumbles on this like old comments have. You can stop the seeker from going through objects by: 1: Making the obstacleProximityPenalty super high, like 50 or something if grass is just 5. This makes the dude steer clear. 2. Adding a physics body to the obstacles and make them kinematic so they don't move. This'll make the seeker unable to move through them and they'll just run into them until they rotate enough. You could also make your seeker a physics body too but that'd take more work.

  • @Singlerity

    @Singlerity

    Жыл бұрын

    My seeker just kept hitting itself on kinematic wall until it went through by magic.

  • @imheretosleep
    @imheretosleep Жыл бұрын

    There is an error when the target and seeker is on the same node

  • @linuxguy1199
    @linuxguy11997 жыл бұрын

    Forth!

  • @Riphtix
    @Riphtix7 жыл бұрын

    do you know how to set up rotation for a 2d game

  • @SebastianLague

    @SebastianLague

    7 жыл бұрын

    +Chase Rivera (Riphtix) I have a 2d version of this project available on my github. I haven't yet updated it with the last few episodes, bit hopefully it helps you.

  • @Riphtix

    @Riphtix

    7 жыл бұрын

    +Sebastian Lague ive been checking it out and it has helped alot so if you could update it soon that would be so helpful cuz the rotation doesnt act properly on 2d and i have no idea how to make working rotation code

  • @adriangluck2926

    @adriangluck2926

    7 жыл бұрын

    I solved this by child object and assigning it the sprite and creating another script (though you could integrate it into one of Sebastian's). The script feeds the parent's transform which I then use to calculate the Z rotation based on those positions (muchlike Sebastian is doing with the targetposition and oldposition) and rotate the child

  • @Genial1990

    @Genial1990

    5 жыл бұрын

    There is no reply yet, so I will answer with piece of code that worked in my case. I am developing 2d top down action game and I do not want rotation in my script. Instead of using Quaternions, I am calculating an angle between my current move direction and direction to next lookpoint. After that I just calculate Lerp for my current moveDirection vector to get new direction. It seems to be working well in 2d, need to look at turn distance, but so far I am happy with my results. here is my code: //moveDirection is initially set to direction towards first point in the path Vector3 targetDirection = (path.lookPoints[pathIndex] - transform.position).normalized; moveDirection = Vector3.Lerp(moveDirection, targetDirection, Time.deltaTime * turnSpeed).normalized; transform.Translate(moveDirection * Time.deltaTime * movementSpeed);

  • @hamza.abdullah807
    @hamza.abdullah8077 жыл бұрын

    First!!