Search Unity

2D RPG AI random movement to chasing, scripting trouble shooting

Discussion in 'Scripting' started by whorobbedme, Aug 18, 2017.

  1. whorobbedme

    whorobbedme

    Joined:
    Oct 29, 2013
    Posts:
    4
    Guys and Gals, I'll be straight up I'm really bad at programing, iv only been at it a while and its mostly copying code from you tube tutorials and altering them to suit my needs.
    I'm looking to get an enemy to wonder randomly and then when the player is close turn and follow them. but I'm having fierce trouble with getting it to move properly and play the animations for the directions its moving.
    Its for an 2D Rpg.
    If someone could help me a little that would be great :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SkeletonControllerAI : MonoBehaviour {
    6.  
    7.     public Transform[] patrolPoints;
    8.    
    9.     Transform currentPatrolPoint;
    10.     int currentPatrolIndex;
    11.  
    12.     public Transform target;
    13.     public float chaseRange;
    14.  
    15.  
    16.     public float awarenessRange;
    17.     public float distanceToTarget;
    18.  
    19.     private Animator anim;
    20.  
    21.     public bool playerUp;
    22.     public bool playerRight;
    23.     public bool playerLeft;
    24.     public bool playerDown;
    25.  
    26.     public float moveSpeed;
    27.  
    28.     public bool isWalking;
    29.  
    30.     public float walkTime;
    31.     private float walkCounter;
    32.     public float waitTime;
    33.     private float waitCounter;
    34.  
    35.     private int WalkDirection; //walk
    36.  
    37.     private Rigidbody2D myRigidbody;
    38.  
    39.  
    40.     // Use this for initialization
    41.     void Start()
    42.     {
    43.  
    44.         myRigidbody = GetComponent<Rigidbody2D>();
    45.  
    46.         waitCounter = waitTime;
    47.         walkCounter = walkTime;
    48.  
    49.         // ChooseDirection();
    50.  
    51.         anim = GetComponent<Animator>();
    52.  
    53.     }
    54.  
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.  
    59.         //check the distance to the player
    60.         distanceToTarget = Vector3.Distance(transform.position, target.position);
    61.  
    62.         //check to see if enemy is aware of the player
    63.        /* if (distanceToTarget > awarenessRange)
    64.         {
    65.             ChooseDirection();
    66.         }*/
    67.  
    68.         //if the player is within the enemys awarenessRange - Chase
    69.         if (distanceToTarget < awarenessRange)
    70.         {
    71.             Chase();
    72.         }
    73.         if (distanceToTarget > awarenessRange)
    74.         {
    75.             ChooseDirection();
    76.  
    77.         }
    78.  
    79.  
    80.         if (isWalking)
    81.         {
    82.             walkCounter -= Time.deltaTime;
    83.  
    84.  
    85.  
    86.             //switch statment
    87.             switch (WalkDirection)
    88.             {
    89.                 case 0:
    90.                     myRigidbody.velocity = new Vector2(0, moveSpeed);
    91.                     playerUp = true;
    92.                     break;
    93.  
    94.                 case 1:
    95.                     myRigidbody.velocity = new Vector2(moveSpeed, 0);
    96.                     playerRight = true;
    97.                     break;
    98.  
    99.                 case 2:
    100.                     myRigidbody.velocity = new Vector2(0, -moveSpeed);
    101.                     playerDown = true;
    102.                     break;
    103.  
    104.                 case 3:
    105.                     myRigidbody.velocity = new Vector2(-moveSpeed, 0);
    106.                     playerLeft = true;
    107.                     break;
    108.  
    109.             }
    110.             if (walkCounter < 0)
    111.             {
    112.                 isWalking = false;
    113.                 walkCounter = waitTime;
    114.  
    115.             }
    116.  
    117.         }
    118.         else
    119.         {
    120.             waitCounter -= Time.deltaTime;
    121.  
    122.             myRigidbody.velocity = Vector2.zero;
    123.  
    124.             if (waitCounter < 0)
    125.             {
    126.                 ChooseDirection();
    127.             }
    128.         }
    129.  
    130.         anim.SetBool("PlayerUp", playerUp);
    131.         anim.SetBool("PlayerRight", playerRight);
    132.         anim.SetBool("PlayerLeft", playerLeft);
    133.         anim.SetBool("PlayerDown", playerDown);
    134.         anim.SetBool("IsWalking", isWalking);
    135.  
    136.  
    137.     }
    138.     public void ChooseDirection()      //walk
    139.     {
    140.         WalkDirection = Random.Range(0, 4); //Floats never pick the top number. 0-4 = 0,1,2,3.
    141.         isWalking = true;
    142.         walkCounter = waitTime;
    143.  
    144.  
    145.     }
    146.  
    147.     void Chase()
    148.     {
    149.        
    150.    
    151.         Vector3 directionToPlayer = target.position - transform.position;
    152.         transform.Translate(directionToPlayer.normalized * Time.deltaTime * moveSpeed);
    153.  
    154.         if (directionToPlayer.x < transform.position.x)
    155.         {
    156.             transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
    157.             playerUp = true;
    158.         }
    159.         if (directionToPlayer.x > transform.position.x)
    160.         {
    161.             transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
    162.             playerRight = true;
    163.         }
    164.         if (directionToPlayer.y < transform.position.x)
    165.         {
    166.             transform.Translate(0, -moveSpeed * Time.deltaTime, 0);
    167.             playerLeft = true;
    168.         }
    169.         if (directionToPlayer.y > transform.position.x)
    170.         {
    171.             transform.Translate(0, moveSpeed * Time.deltaTime, 0);
    172.             playerDown = true;
    173.         }
    174.         transform.Translate(directionToPlayer.normalized * Time.deltaTime * moveSpeed);
    175.     }
    176. }
     
  2. whorobbedme

    whorobbedme

    Joined:
    Oct 29, 2013
    Posts:
    4
    iv managed to solve some of the problem but the enemy is still moving to fast to the left when chasing the player