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 DubstepDragon · Jan 04, 2015 at 07:12 PM · pathfindinggridpathfinding

Grid-based path-finding, similar to Legend of Grimrock/Eye of the Beholder...

I am working on an old-school grid-based first-person dungeon crawler and my prototype is almost complete. The only thing I have to work on is enemy path-finding. My enemy has a wander AI that is active when the FoundPlayer bool is false. He has a box collider that is stretched out really far in order to detect the player GameObject. As soon as I start scripting, I come to a halt. I am completely clueless how to lay this out.

Another thing you may spot is inefficient is the Random.Range to check when to move; however, I had to increase the value so as the enemy does not move as often. If you can suggest a more effective/efficient approach, I'm all ears :)

Here is my enemy movement script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement : MonoBehaviour {
     
     public static bool CanMoveForward;
 
     public bool FoundPlayer;
 
     //Translation:
     float movSpeed = 4.0f;
     Vector3 pos;
     Transform tr ;
     public static bool moving = false;
     
     //Rotation:
     public static bool rotating = false;
     public float rotSpeed = 360f;
     float rotDegrees = 0f;
     Quaternion rotToAngle ;
     
     void Start () {  
         pos = transform.position;
         tr = transform;
 
         CanMoveForward = true;
 
         FoundPlayer = false;
     }
     
     // Update is called once per frame
     void Update () {
 
         int randomValue = Random.Range (1, 100);
 
         Debug.DrawRay(transform.position, transform.forward, Color.red);
 
         if (!moving && !rotating && !FoundPlayer) {
 
             if (randomValue == (1 | 2) && tr.position == pos && CanMoveForward) {
                 pos += transform.forward;
                 moving=true;
             }
 
             else if (randomValue == 5 && tr.position == pos) {
                 rotDegrees -= 90f;
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
             }
 
             else if (randomValue == 6 && tr.position == pos) {
                 rotDegrees += 90f;
                 rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
                 rotating = true;
             }
 
         } else if (FoundPlayer) {
 
         }
         
         
         
         //Translation:
         if (moving) {
             if (Vector3.Distance(transform.position,pos) <0.05f){
                 transform.position = pos;
                 moving=false;
                 //                print ("FINISHED MOVE");
             } else {
                 transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
             }
         }
         
         //Rotation:
         if (rotating) {
             if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
                 transform.rotation = rotToAngle;
                 rotating=false;
                 //                print ("FINISHED TURN");
             } else {
                 transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
             }
         }
     }
     
 }
 


Thanks in advance!

Comment
Add comment · Show 1
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 DubstepDragon · Jan 05, 2015 at 11:28 AM 0
Share

ping ping ping :P

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Jan 05, 2015 at 11:48 AM

For pathfinding you can use any of the existing pathfinding algorithms available like A*. Now A* is graph based algorithm but your grid can be converted to work with graph based algorithms. The article Grid pathfinding optimizations provides some insight on how to use A* or Dijkstra’s Algorithm for grid based pathfinding (This article has a link to another article link text which explains the process in detail) and optimizations. You might want to start with these algorithms for path finding.

Or you can even use NavMesh and translate the path to work with a grid based system. Here you need to convert the path based on your grid and then move your enemy using this newly constructed grid based path.

Another thing you may spot is inefficient is the Random.Range to check when to move; however, I had to increase the value so as the enemy does not move as often.

Rather than performing Random.Range in each Update you can decide a target position for your enemy and then check if your enemy has reached target position and then decide a new position for it to move to. Or you can use Coroutines to decide target position for your enemy after each set time interval.

Comment
Add comment · Show 3 · 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 DubstepDragon · Jan 05, 2015 at 04:45 PM 0
Share

I am worried whether this may break the grid-based nature of the game, as my character is constantly moving... Will it update dynamically?

avatar image DubstepDragon · Jan 05, 2015 at 04:54 PM 0
Share

Also, I'm really new to pathfinding and I didn't understand the websites you included.

avatar image HarshadK · Jan 06, 2015 at 05:23 AM 0
Share

So a good idea might be to go through some tutorials on pathfinding and A* so that you will understand how it works.

avatar image
0

Answer by Cherno · Jan 05, 2015 at 01:18 PM

I would like to put in another suggestion for an existing A* pathfinding solution: Astar Pathfinding project by Aron Granberg, it is available on the Asset store for free, with an advanced version that costs around $100 IIRC. The basic version will probably suffice for your needs and it comes with support for grid-based graphs.

Comment
Add comment · 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

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

grid pathing system 1 Answer

A* pathfinding, Re checking nodes 1 Answer

i want to make a path system like in racing in gtav,i want to make a path system like racing in gta 0 Answers

Find a path with specific steps (moves) on a grid 0 Answers

My "Smooth Rotation" Jerks around randomly when switching between points. 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