Help With NavMesh
I'm making a game for a school project where you have to look at the enemy so he doesn't get to the player but when he does collide with the player, it plays a "GameOver" animation. When I tried to implement NavMesh to the terrain, everything works like the way it did but the collision method doesn't get detected or the animation doesn't play.
Enemy's Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityStandardAssets.Characters.FirstPerson;
public class SCPMovement : MonoBehaviour
{
public float speed = 1f;
public Transform target; //Reference to Player
private NavMeshAgent agent;
public bool isGameOver = false;
private Raycast raycast;
// Use this for initialization
void Start()
{
agent = gameObject.GetComponent<NavMeshAgent>();
raycast = target.GetComponent<Raycast>();
}
// Update is called once per frame
void Update()
{
if (raycast.looking == false)
{
agent.SetDestination(target.position);
float step = speed * Time.deltaTime;
transform.LookAt(target);
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.name == "Player") // creates a condition where if the collidedWith object turns into the gameObject, it will destroy the collidedWith object AM
{
print("Destroyed");
isGameOver = true;
print("Destroyed");
speed = 0f;
raycast.looking = true;
}
}
}
Player's Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycast : MonoBehaviour {
public Transform target;
public bool looking = false;
CharacterController charCtrl;
// Use this for initialization
void Start () {
charCtrl = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Vector3 p1 = transform.position + charCtrl.center;
float distanceToObstacle = 0;
// Cast a sphere wrapping character controller 100 meters forward
// to see if it is about to hit anything.
if (Physics.SphereCast(p1, charCtrl.height * 4 , transform.forward, out hit, 500) && hit.collider.gameObject.CompareTag("gameObject"))
{
looking = true;
print("Hit!");
distanceToObstacle = hit.distance;
}
else
{
looking = false;
}
}
}
isGameOver Checking Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverScript : MonoBehaviour {
Animator anim;
public SCPMovement scpmovement;
public FinishZone finished;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (scpmovement.isGameOver == true)
{
anim.SetTrigger("GameOver");
}
}
}
HEllo.
We are not your $$anonymous$$chers... We will not read all your code to see if you know how to do it.
If you had somethign working, and now is not, is because you changed something....
$$anonymous$$ost important lesson to learn is to DEBUG your code while running. (watch some tutorial) so you will find where the game doesnt do what you want, and wil lsee why.
Bye!