Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pennywise881 · Aug 15, 2017 at 11:01 AM · 2d gamepathfindingsteeringmaze

Having trouble steering gameobject.

Hi there. So I am making a 2d maze game where one of the enemies has to follow the player once it detects the player. I am currently using a steering method to steer the enemy towards the player and also when it detects a wall. I am not using any sort of path finding algorithm, just using steering to make a "dynamic" path finder. But I am having some trouble steering the enemy away from the walls and then resume moving towards the player.

This is what I am doing right now:

  1. follow the player when it is detected

  2. set the enemy's velocity towards the player.

  3. use a raycast to detect if a wall is ahead

  4. if raycast detects the wall, steer the enemy towards the direction it is moving in, then steer it towards the position of the player again. E.g if it is moving up while following the player and hits a wall on the left or right, it will steer up.

I am having a hard time steering the enemy away from the wall and then back towards the player again, because once it steers away from the wall and isn't hitting a wall anymore, it checks where the player is to resume it's pursuit, but then the player goes too far away from the enemy and so it doesn't know where to move towards.

Here is the code:

 void Update()
  {
     RaycastHit2D hit2D = Physics2D.Linecast(transform.position, player.transform.position, 1 << 8);
     if (hit2D)
     {
         string wallTag = hit2D.collider.tag;
         FindMovementDirection(ref wallTag);
         wallHit = true;
     }
     else
     {
         wallHit = false;
     }
  }

 void FindMovementDirection(ref string wallTag)
 {
     int len = 3;
     if (wallTag == "V_Wall")
     {
         if (currentVel.y <= 0) currentDirection.localPosition = new Vector3(0, -len, 0);
         else currentDirection.localPosition = new Vector3(0, len, 0);
     }
     else if (wallTag == "H_Wall")
     {
         if (currentVel.x <= 0) currentDirection.localPosition = new Vector3(-len, 0, 0);
         else currentDirection.localPosition = new Vector3(len, 0, 0);
     }
 }

 void seekSteer()
  {
     float maxForce = 0.8f;
     float maxSpeed = 6;

     if (wallHit)
     {
         desiredVel = currentDirection.position - transform.position;
     }
     else
     {
         desiredVel = player.transform.position - transform.position;
     }
     desiredVel.Normalize();
     desiredVel *= maxSpeed;

     steerVel = desiredVel - currentVel;
     if (steerVel.magnitude > maxForce)
     {
         steerVel.Normalize();
         steerVel *= maxForce;
     }
     currentVel += steerVel;
     if (currentVel.magnitude > maxSpeed)
     {
         currentVel.Normalize();
         currentVel *= maxSpeed;
     }
 }

 void FixedUpdate()
 {
     transform.position += currentVel * Time.fixedDeltaTime;
 }

Another thing that I would like to ask is that, should I be using a path finding algorithm?I am aware that I am not using one and that is because I want the enemy to find it's path dynamically. I also know that path finding and steering go hand in hand most of the time, so am I doing it wrong? Thanks.

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

0 Replies

· Add your reply
  • Sort: 

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

76 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 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 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

Maze Game Pathfinding AI 0 Answers

Avoidance Behaviour 2 Answers

[NEED HELP!!] Path finding for 2D game 0 Answers

How do I make enemies only follow my player character when they are close enough to him?,How do I make it so that an enemy only follows my player when the player character is close enough to the enemy. 0 Answers

Patfinding cant change destination Target 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