Hey there! So, I’ve been working on an enemy prefab for my game, and I’m running into a bit of a frustrating issue. I’ve got this enemy that, when spawned, needs to move directly towards the player. But instead, every time I spawn it, it seems to have a mind of its own and initially moves away from the player, which is the opposite of what I want!
I found myself staring at the screen as the enemy goes on a little detour around the player instead of just charging straight at them. It’s kind of hilarious in a way, but not exactly great for gameplay. I want it to lock onto the player and move towards them with a smooth rotation.
I uploaded a video showing the problem, and it’s pretty clear what’s happening. If you want to check it out, here’s the link: [Video](https://youtu.be/hdGP2L-AEA0?si=3XtArY_s8IfAT-Kp).
Here’s a snippet of the code I’m using for the enemy behavior:
“`csharp
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] private float speed = 5f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
//transform.LookAt(GameManager.Instance.player.position);
Quaternion lookAt = Quaternion.LookRotation(GameManager.Instance.player.position – transform.position, Vector3.up);
transform.rotation = lookAt;
transform.Translate(transform.forward * speed * Time.deltaTime, Space.Self);
}
}
“`
I’m guessing it has something to do with how I’m getting the direction for the enemy to move. It looks like it’s rotating toward the player correctly, but somehow, it’s still choosing to stray away initially. Do I need to adjust how I’m calculating the direction? Or is there something I’ve completely missed here?
I’m hoping someone can lend a hand! Any thoughts or ideas on how to fix this so my enemy behaves as intended? Thanks!
Enemy Movement Issue
It seems like your enemy is having trouble locking onto the player and moving directly towards them. Let’s take a closer look at your code!
From your snippet, it looks like you’re handling the rotation nicely with
Quaternion.LookRotation
, but the movement might be where the issue is coming from.When you use
transform.Translate
withSpace.Self
, it’s moving in the local space of the enemy, meaning that sometimes it might not be going in the direction you expect right after it spawns. Instead, try these changes:Revised Code Snippet
By calculating the direction first and then applying it directly to the position, the enemy should move towards the player as intended!
Also, ensure that the enemy is not colliding with any other objects right after spawn, which could cause it to stray away as well. Maybe play around with the spawn position or check for colliders nearby.
Try it out and see if this fixes the little detour problem! Good luck!
It sounds like your enemy AI is indeed rotating correctly towards the player, but it looks like the initial movement direction might be off. When you use
transform.Translate
withSpace.Self
, it’s moving relative to the enemy’s local forward direction. If the enemy’s rotation isn’t fully aligned with the desired forward vector when it first spawns, it can cause unintended movement. One potential fix is to ensure that the enemy moves directly towards the player by calculating the direction vector first, then applying that vector to the enemy’s movement.You can modify your
Update
function to calculate the direction to the player explicitly and apply that directly without relying on the local forward direction. Replace your existingUpdate
block with the following code:This will ensure your enemy always moves directly towards the player from the start and smoothly rotates to face them. Additionally, using
Space.World
forTranslate
allows the enemy to move in the global direction of the player accurately.