- Home /
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));
}
}
Answer by jim3878 · Jun 13, 2018 at 07:22 AM
It is a tough question... there is two way to solve it
use path finding system(you can find them on asset store if you don't want to use navMesh on 2D)
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.
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
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.
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?
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
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?
that depends on what behavior you want enemy performance.
rayCast -Enemy can't trace player wisely, if there is much obstacle on map. It better for some fps which enemy only patrol randomly.
path_finder-this is best solution for most of game,but just as NoDumbQuestion said it will take whole week to implement.
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.
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)
Your answer
Follow this Question
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