Search Unity

A stalker enemy AI script

Discussion in 'Scripting' started by Legosdoctor, Aug 2, 2015.

  1. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    The game I'm making needs a Monster like slender man, in the sense that he will stalk/ teleport to the player. is there a script for this? thanks in advance!
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    This is a give me a script post. Just a warning - probably best to not post threads like this. People aren't fans of give me script posts. We don't work for free, right?

    Try learning some scripting then try and write the script yourself. When you find problems, ask about them here.

    To get you started, how are your characters and enemies moving? Are you using Unitys navigation system or moving them with your own pathfinding?
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, to be fair, he didn't say "give me a script"... he said "is there a script" (presumably meaning, something publicly available that he can just download and use).

    In which case, the simple answer is: no, there probably is not.
     
    DonLoquacious and BenZed like this.
  4. Legosdoctor

    Legosdoctor

    Joined:
    Jul 23, 2015
    Posts:
    21
    just an FYI m8, I already know python programming, but the fact unity doesn't use this is why i asked, i wasn't asking for a give me, just to see of someone had tips
     
  5. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Ah OK My misunderstanding. Didnt mean to come over as rude, but there are a lot of give me scripts posts on this forum.

    Anyways, as for advice - It depends on how you are moving the bady guy - if you are using the Navigation stuff built into unity it will be very simple to follow the player - as the path finding is handled for you. Are you doing it in 3d?
     
  6. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    If your going to be rude Korno, better not post at all.. .. Legosdoctor asking a pretty straightforward question.
     
  7. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Lego: Unity uses transform like many other programming language.

    For teleport you use :
    ThisLocation.transform.position = new Vector3(xvalue,yvalue,zvalue);
    TargetLocation.transform.position = new Vector3(xvalue,yvalue,zvalue);

    Then ThisLocation.transfrom.position = TargetLocation.transform.position;

    Let me know if you need deeper explanation on this, i be happy to help for free for so little..
     
  8. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
  9. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    I really hoped this was going to be someone asking for the AI from S.T.A.L.K.E.R :)
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @Ironmax, I dig that you're trying to help — I am too, but I really don't think that pointing out Transform.position explains how to make a stalker-style AI. He's asking for a car, and you're showing him where to find some tools and parts bins... things he will eventually need to build his own car, yes, but they're not a car.

    But anyway, I think we're all on the same page here: there is no ready-to-go "stalker enemy AI" script you can just drop in and use, but you can write it yourself, and the tutorials Ironmax pointed out are a great place to start. On the grand scale of things, it's not even very hard, so just dive in, do some tutorials, try to write your own AI, and if you get stuck, post back with some specific questions and we'll try to help!
     
  11. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Again, really easy script demand. Does anyone needs some difficult script? Well basically slender is just a model looking at player and finding a suitable place to teleport.. so basically it goes something like this :
    Code (CSharp):
    1. //Script written by Pratyush Priyadarshi.
    2. //Simple script to teleport Slender. This script is made to be used in an open and plain area like a forest.
    3. //Don't use this script if working with irregular terrain as slender may spawn between terrains.
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7. using UnityEngine.SceneManagement;
    8. using UnityEngine.AI;
    9.  
    10. public class MoveSlender: MonoBehaviour {
    11.  
    12.     [Header("Player Settings")]
    13.     GameObject Player; // gameobject player
    14.     Transform player; // the Object the player is controlling
    15.    
    16.     [Header("Enemy Location Settings")]
    17.     public GameObject Cube; // gameobject cube
    18.     Transform cube; // the cube transform
    19.     Vector3 spawnOrgin; // this will be the transform of the cube
    20.     Vector3 maximum; // max distance in the x, y, and z direction the enemy can spawn
    21.    
    22.     [Header("Enemy Calculations Settings")]
    23.     public float spawnRate = 80.0f; // how often the enemy will respawn
    24.     float distanceToPlayer = 12.5f; // how close the enemy has to be to the player to play music
    25.     public float facePlayerfactor = 20f; // angle of facing player
    26.    
    27.     [Header("Booleans")]
    28.     private bool nearPlayer = false; // use this to stop the teleporting if near the player
    29.     private float nextTeleport = 10.0f; // will keep track of when we to teleport next
    30.  
    31.     void Start() {
    32.         Player = GameObject.FindWithTag("Player"); // find the gameobject with the tag of Player.
    33.         player = Player.GetComponent<Transform>(); // from the gameobject of player get the transform
    34.         cube = Cube.GetComponent<Transform>(); // get the transform from the gameobject Cube
    35.         nextTeleport = spawnRate;
    36.     }
    37.  
    38.     void Update() {
    39.         bool inView = Vector3.Dot(Vector3.forward, player.InverseTransformPoint(transform.position)) > 0; // player is in view
    40.        
    41.         maximum.Set(5, 3.31f, 5f); //  max distance in the x, y, and z direction the enemy can spawn is 5, 3.31, 5
    42.         spawnOrgin.Set(cube.position.x, 3.31f, cube.position.z); // slender's spawn origin
    43.        
    44.         FacePlayer(); // face player no matter what
    45.        
    46.         if (!(nearPlayer || inView)) // only teleport if we are not close to the player and not in it's view
    47.         {
    48.             if (Time.time > nextTeleport) // only teleport if enough time has passed
    49.             {
    50.                 transform.position = new Vector3 (Random.Range(spawnOrgin.x, maximum.x), Random.Range(spawnOrgin.y, maximum.y), Random.Range(spawnOrgin.z, maximum.z)); // teleport
    51.                 nextTeleport += spawnRate; // update the next time to teleport
    52.             }
    53.         }
    54.        
    55.         // The breathing sfx
    56.         if (Vector3.Distance(transform.position, player.position) <= distanceToPlayer) // if near player
    57.         {
    58.             if (GetComponent<AudioSource>() && GetComponent<AudioSource>().clip && !GetComponent<AudioSource>().isPlaying && inView) // play the audio if it isn't playing and slender is in view
    59.             {
    60.             GetComponent<AudioSource>().Play();
    61.             nearPlayer = true;
    62.             }
    63.         }
    64.         else
    65.         {
    66.             if (GetComponent<AudioSource>())  // stop the audio if it is playing and slender isn't in view
    67.             {
    68.             GetComponent<AudioSource>().Stop();
    69.             nearPlayer = false;
    70.             }
    71.         }
    72.     }
    73.    
    74.     // The function to make slender face player
    75.     void FacePlayer()
    76.     {
    77.         Vector3 direction = (player.position - transform.position).normalized;
    78.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    79.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * facePlayerfactor);
    80.     }
    81. }
    I hope this helps! I know.. this won't work with irregular terrain.. but you can edit it :) I have just put a slender kit on itch.io check it out.. it's called Advanced Slender Kit