- Home /
bug Move Automatic
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.
you have to use the physics LAYERS system.
it's critical to game progra$$anonymous$$g !
http://docs.unity3d.com/Documentation/Components/LayerBasedCollision.html
Answer by pognoss · Oct 08, 2012 at 04:03 PM
Ok,i'm reading your link, but i change the the Physics Preference Panel by clicking on Edit->Project Settings->Physics but no change in my problem. Do you can explain to me please?
Your answer
Follow this Question
Related Questions
Enemy move error 2 Answers
How to make enemies stop a certain distance away? 2 Answers
Object moving OnTriggerEnter, please help?! 2 Answers
Move enemy ship down while moving left and right 2 Answers
Moving AI away or towards player not working correctly. 1 Answer