AI move/jump

Hello,

I am trying to make volleyball type game in 2D with capsules as players, I am struggling with the AI part. The player doesn’t jump when the ball is close to ball distance, but it jumps when collision.
I want to be able to create an AI that moves smoothly and jumps when near the ball. Any help?
Many Thanks

    private float moveSpeed = 5;
    private float jumpForce = 5;
    private float playerDistance;

    private Rigidbody2D rb2d;
    private Transform myTransform;
    private Transform target;
    private bool grounded = false;

    void Start()
    {
        myTransform = this.GetComponent<Transform>();
        rb2d = GetComponent<Rigidbody2D>();
        target = GameObject.FindWithTag("Ball").transform;
    }

    void FixedUpdate()
    {
        playerDistance = Vector3.Distance(transform.position, target.position);
       
        if (playerDistance < 6f)
        {
            EnemyChase();
        }
        if (playerDistance < 1f)
        {
            EnemyJump();
        }
    }

    private void EnemyChase()
    {
        myTransform.position += (target.position - myTransform.position).normalized * moveSpeed * Time.deltaTime;
    }

    private void EnemyJump()
    {
        transform.Translate(Vector3.up * jumpForce * Time.deltaTime);
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if(other.gameObject.tag == "Ball")
        {
            EnemyJump();
        }
    }

Hi,

This AI can be easily implemented using Panda BT, a script-based Behaviour Tree engine (I’m the author).

First, we need to define some “tasks” on a MonoBehaviour:

using UnityEngine;
using Panda; // We are using Panda BT.

public class VolleyBallPlayer: MonoBehaviour
{
    public float moveSpeed = 5.0f;
    public float jumpImpulse = 5.0f;
    public float ballJumpRange = 2.0f;
    new Rigidbody rigidbody;
    Transform ball;

    // Use this for initialization
    void Start ()
    {
        ball = GameObject.FindWithTag("Ball").transform;
        rigidbody = this.gameObject.GetComponent<Rigidbody>();
    }

    [Task] // <-- This means we can call this method from a BT script.
    void MoveToBall()
    {
        var direction = (ball.position - this.transform.position).normalized;
        this.transform.Translate(direction * moveSpeed * Time.deltaTime);
    }

    [Task]
    void Jump()
    {
        var task = Task.current;

        // When the task is starting, give a upward velocity to jump.
        if ( task.isStarting )
            rigidbody.velocity = Vector3.up * jumpImpulse;

        // We are done with the jump when we have hit the ground (when the vertical velocity is zero).
        if( Mathf.Approximately( rigidbody.velocity.y, 0.0f) )
            task.Succeed();
    }

    [Task]
    bool IsBallWithinJumpRange()
    {
        return Vector3.Distance(ball.position, this.transform.position) < ballJumpRange;
    }
}

Then, we can use these tasks from a BT script, which describes the very logic of the AI:

tree "Root"
    fallback // Either jump or move to the ball
        sequence
            IsBallWithinJumpRange
            Jump
        while not IsBallWithinJumpRange
            MoveToBall

Let me know if you have any question about using Panda BT.

More information here:
http://www.pandabehaviour.com/

Thank you, will try it out.