Posted Code below. Please use Code-Tags whenever posting code: Using code tags properly - Unity Forum
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fox_AI : MonoBehaviour
{
[SerializeField]
Transform _destination;
static UnityEngine.AI.NavMeshAgent _navMeshAgent;
public float turnSpeed = 20f;
public float curSpeed=1f;
public int TargetX;
public int TargetY;
public GameObject PlayRef;
public int DizzyCountdown;
public LivesCount LivesNum;
public GameObject GameOverScreen;
//public VidPlayer VP;
public Fox_AI ScriptThis;
public UIManager UIScript;
public Animator play_Animator;
Animator m_Animator;
Rigidbody m_Rigidbody;
AudioSource m_AudioSource;
Vector3 m_Movement;
Quaternion m_Rotation = Quaternion.identity;
Material OriginalFrame;
Material BlinkFrame;
int Lives;
public string Colour = "Red";
public bool Dizzy = false;
// Start is called before the first frame update
void Start()
{
// _navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
// SetDestination();
m_Animator = GetComponent<Animator>();
//play_Animator = PlayRef.GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_AudioSource = GetComponent<AudioSource>();
//Set Inital Target X and Y for testing reasons
TargetX = 4; //one tile to the Left of the starting position
TargetY = 5;
//InvokeRepeating("NEMMove",1,1);
ScriptThis = (Fox_AI) GetComponent(typeof(Fox_AI));
//Inital colour setup
//This is because it seams to display the 'default' red fox for a little bit before it flips to the
//white texture when first encountered in a map
Material NormalFrame = Resources.Load<Material>("Materials/FoxTex1");
if (Colour == "Red"){
NormalFrame = Resources.Load<Material>("Materials/FoxTex1");
} else if (Colour == "White"){
NormalFrame = Resources.Load<Material>("Materials/FoxTex2");
}
//
Mesh mesh = GetComponent<Mesh>();
SkinnedMeshRenderer meshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
//get info on current Material
meshRenderer.material = NormalFrame;
}
public void Activation()
{
_navMeshAgent = PlayRef.GetComponent<UnityEngine.AI.NavMeshAgent>();
SetDestination();
}
void SetDestination()
{
// _navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
if (_destination != null)
{
Vector3 targetVector = _destination.transform.position;
_navMeshAgent.SetDestination(targetVector);
}
}
// Update is called once per frame
void FixedUpdate()
{
bool Won = play_Animator.GetBool("Won");
//Debug.Log("Has the Player won? - " + Won);
bool Dizzy = m_Animator.GetBool("isDizzy");
if (Won){
GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
m_Animator.SetBool("isWon", true);
//Debug.Log("Foxes won or Lost!");
} else {
if (!Dizzy){
Vector3 targetVector = _destination.transform.position;//PlayRef.transform.position;
_navMeshAgent.SetDestination(targetVector);
}
}
}
//Back to title
void DizzyEnding()
{
m_Animator.SetBool("isDizzy", false);
Material NormalFrame = Resources.Load<Material>("Materials/FoxTex1");
if (Colour == "Red"){
NormalFrame = Resources.Load<Material>("Materials/FoxTex1");
} else if (Colour == "White"){
NormalFrame = Resources.Load<Material>("Materials/FoxTex2");
}
//
Mesh mesh = GetComponent<Mesh>();
SkinnedMeshRenderer meshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
//get info on current Material
meshRenderer.material = NormalFrame;
GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if(m_Animator.GetBool("isDizzy") == false){
GameObject Play = GameObject.Find("Kinky_Player");
PlayerMovement PM = Play.GetComponent<PlayerMovement>();
PM.Pause = true;
Lives = LivesNum.Lives;
if (PM.PowerUpTime > 0){
Debug.Log("Kinky Power!");
//Play video of Kinky on Red Fox
if (Colour == "Red"){
VidPlayer.LoadVideo("KinkyOnRedFox");
} else if (Colour == "White"){
VidPlayer.LoadVideo("KinkyOnWhiteFox");
}
} else {
if (Lives > 0){
if (Colour == "Red"){
VidPlayer.LoadVideo("RedFoxOnKinky");
} else if (Colour == "White"){
VidPlayer.LoadVideo("WhiteFoxOnKinky");
}
LivesNum.Lives -=1;
} else {
//GameOver
GlobalVars.BGM.Stop();
Time.timeScale = 0;
UIScript.showGOv();
//GameOverScreen.SetActive(true);
}
//Play video of Red Fox on Kinky
//Minus 1 to Kinky 'lifes'
//check to see if more then 0 lives left
}
Debug.Log("Fox Hits Player!");
m_Animator.SetBool("isDizzy",true);
Material DizzyFrame = Resources.Load<Material>("Materials/FoxTexDizzy");
if (Colour == "Red"){
DizzyFrame = Resources.Load<Material>("Materials/FoxTexDizzy");
} else if (Colour == "White"){
DizzyFrame = Resources.Load<Material>("Materials/FoxTex2Dizzy");
}
//
Mesh mesh = GetComponent<Mesh>();
SkinnedMeshRenderer meshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();
//get info on current Material
meshRenderer.material = DizzyFrame;
GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
Invoke("DizzyEnding",6f);
}
}
}
void NEMMove()
{
//Function to move a NEM Character
//first work out where on the grid the NEM is
Vector3 NEMPos = transform.position;
float NEMX = NEMPos.x;
float NEMY = NEMPos.z;
float TileX = 0;
float TileY = 0;
Vector3 currentAngle;
//That above bit should get the X and Y (z) values for the NEM position
//now to calculate the tile position
//I think tile 'cubes' are 10x10 now..
// Debug.Log("X =" + NEMX);
if(((NEMX%10-5) == 0) && ((NEMY%10)-5 == 0)){
TileX = Mathf.Floor((NEMX-5)/10)+1;
TileY = Mathf.Floor((NEMY-5)/10)+1;
Debug.Log("Current X = " +TileX);
Debug.Log("Current Y = " +TileY);
// Debug.Log("On the Tile!");
} else {
//Something else?
//Debug.Log("Not on the tile?!?");
}
if (TargetX == TileX && TargetY == TileY){
//NEM has arrived at TargetX and TargetY tile
Debug.Log("On Target");
//Calulate new TargetX and TargetY
}
//Need to get Current Rotation of Character
currentAngle = transform.localEulerAngles;
//Debug.Log(currentAngle.z);
if (currentAngle.y == 0){
if (TargetY > TileY){
//if the NEM is currently Down from the Target Tile
m_Rigidbody.velocity = transform.forward * curSpeed;
// transform.forward(curSpeed);
//Move forward by the current Speed
Debug.Log("Forward!");
}
if (TargetX < TileX){
transform.localEulerAngles = new Vector3(currentAngle.x,270,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
//turn West
} else if (TargetX > TileX){
transform.localEulerAngles = new Vector3(currentAngle.x,90,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
//turn East
}
} else if (currentAngle.y == 90){
if (TargetY < TileY){
//Turn South
transform.localEulerAngles = new Vector3(currentAngle.x,180, currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
} else if (TargetY > TileY){
//Turn North
transform.localEulerAngles = new Vector3(currentAngle.x,0, currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
}
if (TargetX > TileX){
m_Rigidbody.velocity = transform.forward * curSpeed;
Debug.Log("Forward!");
}
} else if (currentAngle.y == 180){
if (TargetY < TileY){
m_Rigidbody.velocity = transform.forward * curSpeed;
Debug.Log("Forward!");
}
if (TargetX < TileX){
//Turn West
transform.localEulerAngles = new Vector3(currentAngle.x,270,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
} else if (TargetX > TileX){
//Turn East
transform.localEulerAngles = new Vector3(currentAngle.x,90,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
}
} else if (currentAngle.y == 270){
if (TargetY > TileY){
//Turn South
transform.localEulerAngles = new Vector3(currentAngle.x,0,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
} else if (TargetY < TileY){
//Turn North
transform.localEulerAngles = new Vector3(currentAngle.x, 180,currentAngle.z);
Debug.Log(transform.localEulerAngles.y);
}
if (TargetX < TileX){
m_Rigidbody.velocity = transform.forward * curSpeed;
Debug.Log("Forward!");
//move forward by current speed
}
}
}
}