- Home /
Enemy gameObject didn't move
I'm actually working on a game project with Unity3D in C#. I already have a good part of my project, but i'm confronted at a problem with the implementation of enemies. Firstly I'm following this tutorial. So I have a gameobject for enemy with box collider and basically all I need. And there is my script :
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
[SerializeField]
private float rotationSpeed = 180; // In degrees per second
[SerializeField]
private float movementSpeed = 1f; // In units per second
[SerializeField]
private float meshRadius = 1f; // In units
private IEnumerator turnTowardsPlayerCoroutine;
private IEnumerator moveTowardsPlayerCoroutine;
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Player")
{
float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
// Ignore trigger events from the inner colliders
if (playerDistance >= 2f * meshRadius)
{
turnTowardsPlayerCoroutine = TurnTowardsPlayer(collider.transform);
moveTowardsPlayerCoroutine = MoveTowardsPlayer(collider.transform);
StartCoroutine(turnTowardsPlayerCoroutine);
StartCoroutine(moveTowardsPlayerCoroutine);
}
}
}
void OnTriggerExit(Collider collider)
{
if (collider.tag == "Player")
{
float playerDistance = Vector3.Distance(collider.transform.position, transform.position);
// Ignore trigger events from the inner colliders
if (playerDistance >= 2f * meshRadius)
{
StopCoroutine(turnTowardsPlayerCoroutine);
StopCoroutine(moveTowardsPlayerCoroutine);
}
}
}
private IEnumerator TurnTowardsPlayer(Transform player)
{
while (true)
{
Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position, Vector3.up);
targetRotation.x = 0f;
targetRotation.z = 0f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
yield return 0;
}
}
private IEnumerator MoveTowardsPlayer(Transform player)
{
while (true)
{
Vector3 playerDirection = transform.position - player.position;
playerDirection.y = 0;
playerDirection = playerDirection.normalized;
Vector3 deltaMovement = playerDirection * movementSpeed * Time.deltaTime;
int layermask = LayerMask.GetMask("Environment");
Vector3 movingTowards = transform.position - playerDirection*meshRadius + (new Vector3(0f, 0.1f, 0f));
if (Physics.Raycast(movingTowards, Vector3.down, 0.25f, layermask))
{
transform.position -= deltaMovement;
}
yield return 0;
}
}
}
The result is, when I enter in the zone where enemy must attacking me, the enemy rotated towards me but he didn't move. Did I forget something or did something wrong ?
Thanks per advance!
PokeRwOw
Answer by theLittleSettler · Sep 29, 2015 at 07:50 AM
Physics.Raycast is almost certainly false. I've had trouble using layermasks with raycasting before when I've assigned the layermask in code (no doubt due to my lack of understanding), though when I assign it though the inspector its fine.
So remove that if statement to check if that is the cause. If so, then remove the layermask from the raycast so its Physics.Raycast(movingTowards, Vector3.down, 0.25f)...to check if its due to the layer or something else. Continue that process until you solve it.
I deleted the whole condition with Physics.Raycast and the enemy still didn't move... I have really no idea where it may come.
use Debug.Log() to see whether $$anonymous$$oveTowardsPlayer is executing at all...
Your answer
![](https://koobas.hobune.stream/wayback/20220612035359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycasting on mask doesnt work 2 Answers
How does Raycast layer mask work? 1 Answer
JS Dynamic Height Camera vibrating =( 1 Answer
Push object in direction of camera 0 Answers