Search Unity

Need help with this simple AI script

Discussion in 'Scripting' started by tawak3500, May 26, 2016.

  1. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Hey guys. So I wrote this AI script for a monster for my 2d game and for some reason _isGrounded bool is always on even when the character isn't touching the ground. Also when the monster is against the wall it just flies up. Why would it fly up since in order to jump there are 3 criteria ( if (_jumpToAvoid && _canJump && _isGrounded)). Also if you think its messy code let me know how to optimize it. Thanks a ton.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyMovement : MonoBehaviour
    5. {
    6.     public Transform sightEnd, ground, sightBegin, jumpEnd;
    7.     public float speed,jumpPower;
    8.     private Rigidbody2D _rb2d;
    9.     private bool _avoid, _isGrounded, _isFacingRight, _jumpToAvoid, _canJump;
    10.  
    11.     void Awake()
    12.     {
    13.         _rb2d = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         int mask1 = 1 << LayerMask.NameToLayer("Ground");
    19.         int mask2 = 1 << LayerMask.NameToLayer("Obstacle");
    20.         int mask3 = 1 << LayerMask.NameToLayer("Player");
    21.         int combinedMask = mask1 | mask2 | mask3;
    22.  
    23.         Debug.DrawLine(transform.position, sightEnd.position, Color.red);
    24.         _isGrounded = Physics2D.Linecast(transform.position, ground.position, mask1 | mask2);
    25.         _avoid = Physics2D.Linecast(sightBegin.position, sightEnd.position, mask3);
    26.         _jumpToAvoid = Physics2D.Linecast(sightBegin.position, jumpEnd.position, mask1 | mask2);
    27.     }
    28.  
    29.     void FixedUpdate()
    30.     {
    31.         if (_isGrounded)
    32.         {              
    33.             _rb2d.velocity = new Vector2(speed, _rb2d.velocity.y);
    34.             _canJump = true;
    35.  
    36.             if (_avoid)
    37.             {
    38.                 speed = -speed;
    39.                 _avoid = false;
    40.             }
    41.         }
    42.         //====================================
    43.         if (speed < 0 && !_isFacingRight)
    44.             Flip();
    45.         else if (speed > 0 && _isFacingRight)
    46.             Flip();
    47.         //====================================
    48.         if (_jumpToAvoid && _canJump && _isGrounded)
    49.         {
    50.             _rb2d.AddForce(new Vector2(0, jumpPower));
    51.             _canJump = false;
    52.         }
    53.     }
    54.  
    55.     void Flip()
    56.     {
    57.         _isFacingRight = !_isFacingRight;
    58.         Vector3 scale = transform.localScale;
    59.         scale.x *= -1;
    60.         transform.localScale = scale;
    61.     }
    62. }
    63.  
     
  2. traderain

    traderain

    Joined:
    Jul 7, 2014
    Posts:
    108
  3. tawak3500

    tawak3500

    Joined:
    Oct 28, 2013
    Posts:
    77
    Thanks. I've figured out the problem. The players collider was overlapped with the linecast. Thats why _isGrounded was always true.