Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Haxxxxx · Jan 15, 2014 at 04:52 AM · collider 2dtower-defensepathfind

Looking for: Simple pathfinding in 2D Tower Defense game with no objects to collide with?

Im currently building a tower defense game.

This is an example of a level I've built:

alt text

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?

firstmap1.png (314.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SloppyDepot · Jan 15, 2014 at 05:33 AM

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:

![alt text][1]

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. [1]: /storage/temp/20680-waypoints.jpg


waypoints.jpg (372.8 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Haxxxxx · Jan 15, 2014 at 04:55 PM 0
Share

Hello!

Thankyou very much for the response, I've implemented your code and took out the BaseLevelScript aspect, also adding in the Waypoints empty game object and filling it with Waypoint empty game objects following my path as you described. Unfortunately it doesn't seem to be doing as expected.

I've stepped through it and it seems to be working out the values of the distances correctly however my character simply moves downwards slowly at a slight angle until running off the screen.

Any idea what this could be?

avatar image Haxxxxx · Jan 15, 2014 at 07:00 PM 0
Share

Did you set the parent Waypoints object to be a waypoint as well or does it not matter where that is located?

avatar image Haxxxxx · Jan 15, 2014 at 07:39 PM 0
Share

OHHHHHHHHHHHHHHHHH. I just thought of Gravity! Very silly of me. Thanks so much for all your help, +1.

avatar image SloppyDepot · Jan 16, 2014 at 03:23 AM 0
Share

Oops, yea... I forgot to mention disabling the gravity. Glad you found it helpful though!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Tower Target Random Tank 1 Answer

Multiple Animated Objects Causing Lag! 0 Answers

Why is my game pausing? 2 Answers

Generating random tower defense level by placing random square-shaped tiles? 1 Answer

Platform pathfinding 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges