Question by
stshamster · May 15, 2020 at 08:26 PM ·
scripting problemmovementcharacterenemy
make something move when not looking at it
I cant find a way to do this. I want my enemy to move when it is not looking at it. i have this. it kinda works. if you don't look at it. It wont move. If you look at it it wont move. You have to be far away. and even if you are far away and looking at it. It moves until a certain range. How can i fix this? This is the program.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SCP173 : MonoBehaviour { Transform playerTransform; UnityEngine.AI.NavMeshAgent myNavmesh; public float checkRate = 0.01f; float nextCheck; public bool NotLookedAt; public float speed = 10f;
void Start()
{
if(GameObject.FindGameObjectWithTag("Player").activeInHierarchy)
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
myNavmesh = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
myNavmesh.speed = speed;
}
void Update()
{
if(Time.time > nextCheck)
{
nextCheck = Time.time + checkRate;
FollowPlayer();
}
if(GetComponent<Renderer>().isVisible)
{
NotLookedAt = false;
}
if(!GetComponent<Renderer>().isVisible)
{
NotLookedAt = true;
}
if(NotLookedAt == true)
{
speed = 100f;
myNavmesh.speed = 100f;
}
if(NotLookedAt == false)
{
speed = 0f;
myNavmesh.speed = 0;
}
}
void FollowPlayer()
{
myNavmesh.transform.LookAt(playerTransform);
myNavmesh.destination = playerTransform.position;
}
}
Comment