• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
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

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

150 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to teleport from scene to other scene? 1 Answer

Teleportation with gun 2 Answers

Teleporting a FPSController Character Instantly teleports back to the previous Position 0 Answers

Google VR: Teleport the GVRMain Camera on a plane after looking on specified Cube to another Plane 0 Answers

How to make a laser pointer with WebXR 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges