Hi everybody!
I have created an enemy. This character can move only on axis X ( so left or right ). If this character has detected the player, he can change the direction and accelerate. It’s ok. But if the enemy is blocked between the player and an other object, he bug and he can’t move after. The problem is my code or others?
My scripts here:
Script Movement:
using UnityEngine;
using System.Collections;
public class MoveEnemy : MonoBehaviour {
public GameObject mesh;
public Texture2D[] texMove;
private int selectedTex;
public float texUpdateTime;
private float tmpTexUpdateTime;
public float moveSpeed;
public static float move;
public static Vector3 direction;
private Transform player;
private Vector3 relative;
private Transform character;
// Use this for initialization
void Start () {
move = moveSpeed;
direction = Vector3.right;
player = GameObject.FindWithTag ("Player").transform;
}
// Update is called once per frame
void Update () {
MoveUpdate();
TexUpdate();
}
void TexUpdate()
{
// temporisation entre les texture (0,15s)
// on change pas de texture tant que les 0,15s ne sont pas écoulé
if (tmpTexUpdateTime > texUpdateTime)
{
tmpTexUpdateTime = 0; // remise à 0 du temps d'attente entre les textures
selectedTex++; // on passe au mouvement suivent;
if (selectedTex > texMove.Length - 1) // si on dépasse le nombre d'image du tableau 'texMove'
{
selectedTex = 0; // on remet à la selection à 0
}
mesh.renderer.material.mainTexture = texMove[selectedTex]; // on ajoute la texture séléctionné
}
else
{
tmpTexUpdateTime += 1 * Time.deltaTime; // temps d'attente en fonction du temps
}
}
void MoveUpdate(){
// Mouvement de l'enemy
transform.Translate( direction * move * Time.deltaTime);
// changement de coté en fonction de la position
mesh.transform.localScale = new Vector3(direction.x,1,1);
transform.localScale = new Vector3(direction.x,1,1);
}
}
Script IA:
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
private Transform player;
private Transform character;
private bool insideInterestArea = true;
private float LefOrRight;
public static bool spotted;
RaycastHit hit;
void Start () {
character = transform;
player = GameObject.FindWithTag ("Player").transform;
}
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.transform == player && CanSeePlayer())
{
OnSpotted();
}
}
bool CanSeePlayer(){
Vector3 playerDirection = (player.position - character.position);
Physics.Raycast (character.position, playerDirection, out hit, playerDirection.magnitude);
if (hit.collider && hit.collider.transform == player) {
return true;
}
return false;
}
void OnSpotted(){
MoveEnemy.direction.x *= -1;
MoveEnemy.move *= 2;
spotted = true;
}
}
Thanks you for yours answers.