- Home /
Can't change direction when enemy collides with obstacle
I'm having trouble getting my enemy game object to change direction when it collides with an obstacle. I have it set up to move randomly, and will change direction after a certain amount of time has passed, but when I try to use OnCollisionEnter to detect collisions and change its direction nothing happens. It keeps trying to move in the same direction until the time passes and it changes direction that way. I have researched and tried to implement the solutions I found, but nothing has worked. Also, I'm pretty new to coding, and I have been able to get as far as I have by cobbling together different bits of code, so the script may be a bit Frankenstein-y, that may be part of the problem.
Any help/advice is much appreciated. My script is below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Controller : 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()
{
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}
void Update()
{
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime)
{
latestDirectionChangeTime = Time.time;
calcuateNewMovementVector();
}
//move enemy:
transform.position = new Vector2(transform.position.x + (movementPerSecond.x * Time.deltaTime),
transform.position.y + (movementPerSecond.y * Time.deltaTime));
var pos = Camera.main.WorldToViewportPoint(transform.position);
pos.x = Mathf.Clamp01(pos.x);
pos.y = Mathf.Clamp01(pos.y);
transform.position = Camera.main.ViewportToWorldPoint(pos);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Obstacle")
{
movementDirection *= -1;
}
}
}
Answer by haidao0923 · Jul 25, 2018 at 04:17 AM
that is because eventhough you modified movementDirection, it is not updated in the update function.
The only other occurence of "movementDirection" is in the if (Time.time - latestDirectionChangeTime > directionChangeTime)
as a result it will only change when the change time is reached
O$$anonymous$$ that makes sense, but forgive me for asking, what would be the best way to include this in the update function? I've tried adding it there, but I still get the same result, which is the enemy object pushing up against the obstacle until the change time is reached?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Enemy wont damage player 0 Answers
onCollisionEnter() look like it don't work 1 Answer
Collision issue with SetParent (C#) 1 Answer