Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problems with AI 2D

Discussion in '2D' started by Bybase, Oct 9, 2019.

  1. Bybase

    Bybase

    Joined:
    Sep 4, 2019
    Posts:
    1
    First of all sorry for my english.
    Im triyng to do kind of platform game with some storytelling and that.
    Now im working in the AI system, i dont want the common patrol system, i want a Little more, but it is making me stress xd cause im new on this.
    So, i do some sphere gizmos where if the player enters, the AI will folllow it unitl it goes out the sphere.
    My problem is that i cant do it jump well. My idea was to put a collider that if it not detects ground, it jumps. Here is the AI script:

    Code (CSharp):
    1. public class EnemyMovement : MonoBehaviour
    2. {
    3.     public float lookRadius;
    4.     public Transform target;
    5.     public float speed = 2f;
    6.     public float maxSpeed;
    7.     public bool grounded;
    8.     public bool follow;
    9.  
    10.     private GameManager gameManagement;
    11.     private Animator anim;
    12.     public static Rigidbody2D rb2d;
    13.  
    14.     private void Start()
    15.     {
    16.         target = GameObject.FindGameObjectWithTag("Player").transform;
    17.         gameManagement = GameObject.Find("GameManager").GetComponent<GameManager>();
    18.         rb2d = GetComponent<Rigidbody2D>();
    19.  
    20.         anim.SetBool("isFollowing", follow);
    21.         anim.SetBool("Grounded", grounded);
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         float distance = Vector3.Distance(target.transform.position, this.transform.position);
    27.  
    28.         if (distance <= lookRadius)
    29.         {
    30.             follow = true;
    31.             if (grounded)
    32.             {
    33.                 transform.position = Vector2.MoveTowards(new Vector2(transform.position.x, transform.position.y), target.position, speed * Time.deltaTime);
    34.             }
    35.         }else
    36.         {
    37.             follow = false;
    38.         }
    39.     }
    40.     private void FixedUpdate()
    41.     {
    42.         Vector3 fixedVelocity = rb2d.velocity;
    43.         fixedVelocity.x *= 0.90f;
    44.         if (grounded)
    45.         {
    46.             rb2d.velocity = fixedVelocity;
    47.         }
    48.         float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
    49.         rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
    50.     }
    51.  
    52.     private void OnCollisionStay2D(Collision2D collision)
    53.     {
    54.         if (collision.gameObject.tag == "Ground")
    55.         {
    56.             gameManagement.EnemyOnGroundStay();
    57.         }
    58.     }
    59.     private void OnCollisionExit2D(Collision2D collision)
    60.     {
    61.         if (collision.gameObject.tag == "Ground")
    62.         {
    63.             gameManagement.EnemyOnGroundExit();
    64.         }
    65.     }
    66.  
    67.     private void OnDrawGizmosSelected()
    68.     {
    69.         Gizmos.color = Color.red;
    70.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    71.     }
    72. }
    And Here is the Script that tries to make it jump xd :

    public class CheckJump : MonoBehaviour
    {
    public bool jump;
    public float jumpPower;
    private void FixedUpdate()
    {
    if (jump)
    {
    EnemyMovement.rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
    jump = false;
    }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
    if(collision.gameObject.tag == "Ground")
    {
    jump = true;
    }
    }
    }

    I supose that the problem is the physics, but i dont know

    If someone could help me i will be grateful :)
     
  2. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    21
    To make an enemy jump a platform is difficult. Because you need to know the right momentum. If not, the enemy will jump, but perhaps the force or speed is not enough and then it will fall. To solve this, you can use NavMesh. But you need to work on the X and Z axes.
    But if you want to do it without NavMesh. You can put points where the enemy needs to jump. And for the logic of the jump you need to apply physics, in this case the projectile motion where you need to know the Vo (Vx and Vyo) for the initial impulse. And this value is assigned to speed. AddForce in this case no, because the result of this will be low.

    And, of course, angles are required for projectile motion. Then you need to apply vectors. Get the distance between the two points (origin and destination) and use the trigonometry functions to obtain the angle generated by the two points

    Formula Vo (Vx and Vyo)

    Vx = x / cos(angle / 180 * Mathf.PI)

    Vyo = y / sin(angle / 180 * Mathf.PI) + 1/2 * Mathf.Abs (Physics2D.gravity.y)

    -9.86 = Physics2D.gravity.y

    For to know more about this. See the projectile motion and vector formula
     
    MisterSkitz likes this.