- Home /
Enemy only moves in a line
I am trying to make my enemy move towards me but for some reason they head towards me for one second then doesn't change the direction they go. It currently has an Animator, box collider, rigidbody, and a capsule collider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mar_Tracker : MonoBehaviour
{
public Transform Player;
public float MoveSpeed = 3.0f;
public float InRadius = 250.0f;
public float AttackRange = 11.0f;
public float rocketRange = 50.0f;
private Animator anim;
private Coroutine RocketLouch = null;
public GameObject Rocket;
public GameObject Explosion;
public SphereCollider sphereCollider;
void Start()
{
// Set it at the start to optimize performance
anim = GetComponent<Animator>();
Player = GameObject.FindGameObjectsWithTag("Player")[0].transform;
sphereCollider.enabled = false;
}
// Use fixed Update for moving things around
// Better performance
void FixedUpdate()
{
HandlePlayerDetection();
}
void HandlePlayerDetection()
{
transform.LookAt(Player); // Makes it so that the enemy looks at player
// Find the distance between the player and the transform
var distance = Vector3.Distance(transform.position, Player.position);
// Do the boolean calculations
bool inRadius = distance <= InRadius;
bool inAttackRange = distance <= AttackRange;
bool inRocketRange = distance <= rocketRange;
anim.SetBool("Attacking", inAttackRange);
anim.SetBool("InArea", inAttackRange);
// Lets use inbuilt functions to movement
if (inRadius)
{
//transform.position += transform.forward * MoveSpeed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(transform.position, Player.position, MoveSpeed * Time.deltaTime); neither of these methods work
}
// Rocket code
if (inRocketRange)
{
FireRocket();
}
}
void FireRocket()
{
if (RocketLouch == null)
{
RocketLouch = StartCoroutine(RocketLaunch());
}
}
IEnumerator RocketLaunch()
{
anim.SetBool("Rocket", true);
yield return new WaitForSeconds(0.15f);
sphereCollider.enabled = true;
Explosion.SetActive(true);
yield return new WaitForSeconds(1.0f);
anim.SetBool("Rocket", false);
Destroy(Rocket);
}
}
Answer by triangle4studios · Jun 17, 2021 at 11:52 PM
What you are seeing, is the built-in forward translation of your animation. You should remove that, as it will interfere with everything.
If you used Mixamo, I recommend going back and making sure you set your animation up to walk in place.
After:
transform.LookAt(Player); // Makes it so that the enemy looks at player
Try adding
float speed = 10;
transform.forward * speed * Time.deltaTime;
Or if you are using 2D:
float speed = 10;
transform.up * speed * Time.deltaTime;
Unless the enemy is inside the radius, he will not move. So you need this somewhere outside of the radius check.
I strongly recommend working without animations until you get the basics down. Best wishes.
Andrew
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Rotate towards movement direction 1 Answer
Multiple Cars not working 1 Answer
Having trouble with grid movement 1 Answer
Best way to move my player (3d) 1 Answer