Im currently building a tower defense game.
This is an example of a level I’ve built:
Now I’ve looked in to pathfinding using Unity 2d and have pretty much come up empty apart from the notorious A* pathfinding. If I’m right in thinking it’s rather complex, being a beginner programmer I wouldn’t like to exactly delve in at the deep end if their is a simpler solution to my problem. Is their a method in Unity where I can spawn enemies at the red point and have them walk fairly centralized to the end point?
I don’t particularly what any code etc I just want to get an idea of how to go about it. Any help appreciated!
Edit: Ive been made aware of Navmeshes however are they not a pro only feature?
If I understand you correctly, you want to define a path and have some characters walk it. If that’s the case, A* wouldn’t really be necessary (since you don’t need to find the path, you just need to walk it).
I’ll share what I’ve done for my tower defense game.
After placing the level sprite in the scene, I created an empty game object called “Waypoints” which will act as the parent object for all the individual waypoints.
Then, I started creating the individual waypoints (which are also just empty game objects). The order in which they’re placed is important, so just trace the path from start to finish. You should have something that looks like this when you’re done:
Then, on my character’s game object (which needs to have a Rigidbody2D component), I added the following script component:
public class FollowWaypointsScript : MonoBehaviour
{
private int _targetWaypoint = 0;
private BaseLevelScript _levelScript;
private Transform _waypoints;
public float movementSpeed = 3f;
// Use this for initialization
void Start ()
{
_levelScript = GameObject.Find("LevelScript").GetComponent<BaseLevelScript>();
_waypoints = GameObject.Find("Waypoints").transform;
}
// Update is called once per frame
void Update ()
{
}
// Fixed update
void FixedUpdate()
{
handleWalkWaypoints();
}
// Handle walking the waypoints
private void handleWalkWaypoints()
{
Transform targetWaypoint = _waypoints.GetChild(_targetWaypoint);
Vector3 relative = targetWaypoint.position - transform.position;
Vector3 movementNormal = Vector3.Normalize(relative);
float distanceToWaypoint = relative.magnitude;
float targetAngle = Mathf.Atan2(relative.y, relative.x) * Mathf.Rad2Deg - 90;
if (distanceToWaypoint < 0.1)
{
if (_targetWaypoint + 1 < _waypoints.childCount)
{
// Set new waypoint as target
_targetWaypoint++;
}
else
{
// Inform level script that a unit has reached the last waypoint
_levelScript.reduceHearts(1);
Destroy(gameObject);
return;
}
}
else
{
// Walk towards waypoint
rigidbody2D.AddForce(new Vector2(movementNormal.x, movementNormal.y) * movementSpeed);
}
// Face walk direction
transform.rotation = Quaternion.Euler(0, 0, targetAngle);
}
}
(Note: you can ignore the BaseLevelScript parts… They’re specific to my game.)
Basically, what the script is doing is:
- Setting the initial target waypoint to 0
- Each time FixedUpdate() is called:
- Check the distance between the character’s current position and the waypoint’s position.
- If the distance is less than 0.1, the character is close enough to the target waypoint to switch to the next waypoint (if one exists).
- If the distance is larger than 0.1, the character walks towards it using rigidbody2D.AddForce.
This works pretty well for me.
I should also mention that I have Linear Drag set to 10 on my character’s rigidbody.
Hope this helps.