Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help needed for a slender script.

Discussion in 'Scripting' started by eric56379, Oct 27, 2014.

  1. eric56379

    eric56379

    Joined:
    Mar 11, 2014
    Posts:
    89
    So I am trying to create this slender game and I need it done soon. I have the following code so far. (Btw, I'm a beginner):
    Code (JavaScript):
    1. var target : Transform; //the enemy's target
    2. var moveSpeed = 3; //move speed
    3. var rotationSpeed = 3; //speed of turning
    4. var range : float=10f;
    5. var range2 : float=10f;
    6. var stop : float=0;
    7. var myTransform : Transform; //CURRENT TRANSFORM data of this enemy
    8. function Awake()
    9. {
    10.     myTransform = transform; //cache transform data for easy access/preformance
    11. }
    12.  
    13. function Start()
    14. {
    15.     target = GameObject.FindWithTag("Player").transform; //target the player
    16.    
    17. }
    18.  
    19. function Update () {
    20.     //rotate to look at the player
    21.     var distance = Vector3.Distance(myTransform.position, target.position);
    22.     if (distance<=range2 &&  distance>=range){
    23.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    24.                                                 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    25.     }
    26.    
    27.    
    28.     else if(distance<=range && distance>stop){
    29.        
    30.         //move towards the player
    31.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    32.                                                 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    33.         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    34.     }
    35.     else if (distance<=stop) {
    36.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    37.                                                 Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    38.     }
    39.    
    40.    
    41. }
    So what this code does is making something follow you. I am trying to make it that when you look directly at the object, it stops moving. When you look away, it can get closer, then when you touch it, it brings you to a level called "MainMenu". Do you know how I can do this?
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    He said that game goes back to main menu when player touches slender man, not within specific range.

    @eric56379 I suggest you to look into triggers.
     
  4. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417