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 SuperCrow2 · Jun 13, 2018 at 05:49 AM · movingthrough

Enemy goes through walls

I wanted the enemy to move randomly which is does with code below, but how do I get it so it doesn't go through walls? I think someone said I need to use force? Where do I include that and what will it look like? I am using Unity 2d.

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class EnemyMovement : MonoBehaviour
 {
 
     private float latestDirectionChangeTime;
     private readonly float directionChangeTime = 3f;
     private float characterVelocity = 2f;
     private Vector2 movementDirection;
     private Vector2 movementPerSecond;
 
 
     void Start()
     {
         latestDirectionChangeTime = 0f;
         calcuateNewMovementVector();
     }
 
     void calcuateNewMovementVector()
     {
    
         movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
         movementPerSecond = movementDirection * characterVelocity;
     }
 
     void Update()
     {
         
         if (Time.time - latestDirectionChangeTime > directionChangeTime)
         {
             latestDirectionChangeTime = Time.time;
             calcuateNewMovementVector();
         }
 
       
         transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
         transform.position.y + (movementPerSecond.y * Time.deltaTime));
 
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jim3878 · Jun 13, 2018 at 07:22 AM

It is a tough question... there is two way to solve it

  1. use path finding system(you can find them on asset store if you don't want to use navMesh on 2D)

  2. use 2 raycast to detective distance and direction of wall .If there is a wall in front of enemy ,Set a new direction to enemy.

Comment
Add comment · Show 14 · 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 NoDumbQuestion · Jun 13, 2018 at 07:29 AM 0
Share

I press downvote by mistake and you suddenly have 2 downvotes and i can only cancel one.(Unity html error i guess)

Anyway, another option is using 2D collider. And, you should use force velocity to move character. 2D Collider do work with transform.SetPosition if you have big wall and character cannot glitch through wall

avatar image jim3878 NoDumbQuestion · Jun 14, 2018 at 02:24 AM 0
Share

I don't suggest using 2D collider ,because it make enemy down speed when enemy touch wall. So,if you don't make your enemy looks dumb , resetting direction of enemy is very important.

avatar image SuperCrow2 · Jun 14, 2018 at 02:42 AM 0
Share

How do I use nav$$anonymous$$esh? And where would it be inserted in the code in the OP?

Or will it better to use raycast?

avatar image NoDumbQuestion SuperCrow2 · Jun 14, 2018 at 02:59 AM 0
Share

for dummy looking 2D game, raycast and X,Y coordinate control is much better and cheaper.

For better way-path finder for AI you have to work with A* pathfinder(free on UnityAsset). All AI will know where to go and never go out of bound.

That depends on how much time you got to finish this project. Invest time in researching A* could take whole week to implement

avatar image SuperCrow2 NoDumbQuestion · Jun 14, 2018 at 03:04 AM 0
Share

This game is not a very advanced game so I don't need to to get too technical with it. All I need it to be able to do is not pass through walls. So all I need to do is put in the coordinates of the wall?

Show more comments
avatar image jim3878 SuperCrow2 · Jun 14, 2018 at 03:30 AM 0
Share

that depends on what behavior you want enemy performance.

  1. rayCast -Enemy can't trace player wisely, if there is much obstacle on map. It better for some fps which enemy only patrol randomly.

  2. path_finder-this is best solution for most of game,but just as NoDumbQuestion said it will take whole week to implement.

avatar image SuperCrow2 jim3878 · Jun 14, 2018 at 03:34 AM 0
Share

It's not an FPS game, I just need it so the enemy changes direction once it hits a wall. Think of it as a ball rolling around randomly and you need to avoid it but you don't want the ball to move outside of the main play area because then there will be no ball to dodge.

Show more comments
avatar image
-1

Answer by agarcia115 · Jun 13, 2018 at 06:27 AM

Maybe adding a collider to your enemies? I suppose nav mesh is not an option because it's 2D (i never used nav mesh on a 2d environment)

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

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

Related Questions

My player glitching on moving collider 0 Answers

how to make my player move through enemies?,how to make my player move through enemies 2 Answers

moving gun in first person shooter while walking 8 Answers

Controlling a Character with analog stick causes odd idle glitch 1 Answer

are there some way to determine whether the mouse is moving or not. 2 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