Question by
BeanSlice · Aug 01, 2017 at 02:10 PM ·
aiteleportteleportingslender
Struggling with creating a Slender-like AI (Creating this for educational purposes)
Okay, first of all, I am aware that there are tons of scripts for a Slender AI, but I want to recreate it myself so I can learn in the process all about AI and also I am curious. I am struggling to: get the AI to teleport close to the player, and to teleport away from player when the player is not looking at the AI. Here is my script. Can anybody help me out with the telporting, and also maybe a different method of roaming the map which involves random positions instead of waypoints set around the map? Thanks :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.AI;
public class SlenderAI : MonoBehaviour
{
//waypoint variables
public float timeBetween;
private int waypointInd;
public GameObject [] waypoints;
//Raycast variables
public string targetName;
public RaycastHit hit;
public float theDistance;
//Target variables
public float targetDistance;
public raycast playerRaycast;
public Transform theTarget;
public FirstPersonController targetController;
public Rigidbody targetRigidbody;
//Movement variables
private NavMeshAgent enemyAgent;
public float damping = 5f;
//Enemy variables
public int moveSpeed;
public int maxDistanceFromPlayer;
private Renderer enemyRenderer;
private Rigidbody enemyRigidbody;
public float lookDistance;
public int attackDistance;
void Start ()
{
enemyRigidbody = GetComponent<Rigidbody> ();
enemyRenderer = GetComponent<Renderer> ();
enemyAgent = GetComponent<NavMeshAgent> ();
playerRaycast = GetComponent<raycast> ();
targetController = GetComponent<FirstPersonController> ();
}
void FixedUpdate ()
{
targetDistance = Vector3.Distance (theTarget.position, transform.position);
Raycast ();
if (targetDistance > lookDistance)
{
Idle ();
}
if (targetDistance < lookDistance)
{
StopCoroutine (TeleportRoam ());
LookAtTarget ();
if (targetDistance < attackDistance)
{
Attack ();
}
}
}
void Raycast ()
{
Vector3 fwd = transform.TransformDirection (Vector3.forward) * 20;
Debug.DrawRay (transform.position, fwd, Color.red);
if (Physics.Raycast (transform.position, fwd, out hit))
{
theDistance = hit.distance;
//print (hit.distance + " " + hit.collider.gameObject.name);
if (hit.collider.gameObject.name == targetName)
{
//print ("Raycast has detected " + targetName);
}
}
}
void LookAtTarget ()
{
enemyAgent.speed = 0;
enemyRenderer.enabled = true;
Quaternion rotation = Quaternion.LookRotation (theTarget.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.fixedDeltaTime * damping);
if (targetDistance > lookDistance)
{
Idle ();
}
}
IEnumerator TeleportRoam ()
{
waypointInd = Random.Range (0, waypoints.Length);
enemyAgent.SetDestination (waypoints [waypointInd].transform.position);
yield return new WaitForSeconds (timeBetween);
waypointInd = Random.Range (0, waypoints.Length);
enemyAgent.SetDestination (waypoints [waypointInd].transform.position);
}
void Roaming ()
{
StartCoroutine (TeleportRoam ());
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Waypoint"))
{
Roaming ();
}
}
void Idle ()
{
enemyAgent.speed = moveSpeed;
enemyRenderer.enabled = false;
enemyAgent.SetDestination (waypoints [waypointInd].transform.position);
}
void Attack ()
{
enemyRenderer.enabled = false;
//if (targetDistance < maxDistanceFromPlayer)
//{
//this.transform.position = new Vector3 (theTarget.position.x + 1, theTarget.position.y - 1, theTarget.position.y);
//enemyRenderer.enabled = true;
//}
if (hit.collider.gameObject.name == this.gameObject.name)
{
DontMove ();
}
if (!(hit.collider.gameObject.name == this.gameObject.name))
{
Move ();
}
}
void DontMove ()
{
enemyAgent.speed = 0;
}
void Move ()
{
this.transform.position = new Vector3 (theTarget.position.x + 1, theTarget.position.y, theTarget.position.z + 1);
}
}
Comment