I don’t understand why this is happening. Is this a known issue with Unity? The models of the enemies I have made are facing the opposite direction of what I need them to be facing.
Edit: I feel it should be known that the enemies when spawned are facing the correct direction, charging down the z axis towards the player and while the LookAt() hasn’t been reached. Once it got to that point the enemies turned 180 degrees away from the player.
Here are two scripts I use for the enemies to face the character and to use animations:
using UnityEngine;
using System.Collections;
public class EnemyAIMelee : MonoBehaviour
{
public float targetDistance = 20.0f;
public float attackDistance = 10.0f;
public float enemySpeed = -40.0f;
public float chaseSpeed = 1.0f;
private bool spawningIn = true;
private bool unlockAI = false;
public GameObject playerCharacterManager;
void Start()
{
playerCharacterManager = GameObject.FindGameObjectWithTag ("PlayerCharacterManager");
}
// Update is called once per frame
void Update ()
{
SpawnIn ();
MovingAI ();
}
void SpawnIn()
{
if (spawningIn == true)
{
GameObject plane = GameObject.FindGameObjectWithTag("PlayerPlane");
if (transform.position.z - plane.transform.position.z <= targetDistance)
{
Debug.Log ("Made it to the playing field");
SendMessage("EnemyMovingAnimation", false);
transform.parent = plane.transform;
unlockAI=true;
spawningIn = false;
}
else
{
SendMessage("EnemyMovingAnimation", true);
transform.position += transform.forward*enemySpeed*Time.deltaTime;
}
}
}
//This is causing me all the problem
void MovingAI()
{
if(unlockAI == true)
{
Debug.Log ("MovingAI has been reached");
float chasingSpeed = chaseSpeed * Time.deltaTime;
transform.LookAt(playerCharacterManager.transform);
transform.position = Vector3.MoveTowards (transform.position, playerCharacterManager.transform.position, chasingSpeed);
SendMessage ("EnemyMovingAnimation", true);
float dist = Vector3.Distance(playerCharacterManager.transform.position, transform.position);
if(dist<=attackDistance)
{
AttackAI();
}
}
}
void AttackAI()
{
unlockAI =false;
SendMessage ("EnemyAttackAnimation", true);
Debug.Log ("AttackAI has been reached");
unlockAI =true;
}
}
The second one.
using UnityEngine;
using System.Collections;
public class AllEnemyAnimations : MonoBehaviour
{
Animator animator;
float Mmoving;
float Aattack;
bool enemyAttack;
bool enemyMoving;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator> ();
enemyAttack = false;
enemyMoving = false;
}
// Update is called once per frame
void Update ()
{
Moving ();
Attack();
Death ();
}
void FixedUpdate()
{
animator.SetFloat ("Moving", Mmoving);
animator.SetFloat ("Attack", Aattack);
animator.SetBool ("Died", false);
}
void Moving()
{
if(enemyMoving)
{
Mmoving = 0.2f;
}
else
{
Mmoving = 0.0f;
}
}
void Attack()
{
if(enemyAttack)
{
Aattack = 0.2f;
}
else
{
Aattack = 0.0f;
}
}
void Death()
{
EnemyHealth enemyHealth = GetComponent<EnemyHealth>();
float EnemyHealth = enemyHealth.currentHealth;
if (EnemyHealth <= 0)
{
animator.SetBool ("Died", true);
}
}
void EnemyMovingAnimation(bool enemyMovingStatus)
{
enemyMoving = enemyMovingStatus;
}
void EnemyAttackAnimation(bool enemyAttackStatus)
{
enemyAttack = enemyAttackStatus;
}
}
I would appreciate it if someone were to point out what is inherently wrong with this. I think I've pulled enough of my hair out as it is.