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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question AI Pathfinding(Jumping) 2D

Discussion in '2D' started by marius234582, Dec 15, 2020.

  1. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    Hello,

    I have a little issue with my AI which is jumping randomly with no sense at all.
    When I move around its following normaly, but when I jump over it, then it starts to jump like 2-4 times in a row and I have no idea why. Would someone please take a look at my script and tell me what could be wrong?

    [Header("Pathfinding")]
    public Transform target;
    public float activateDistance = 50f;
    public float pathUpdateSeconds = 0.5f;

    [Header("Physics")]
    public float speed = 200f;
    public float nextWaypointDistance = 3f;
    public float jumpNodeHeightRequirement = 0.8f;
    public float jumpModifier = 0.3f;
    public float jumpCheckOffset = 0.1f;

    [Header("Custom Behavior")]
    public bool followEnabled = true;
    public bool jumpEnabled = true;
    public bool directionLookEnabled = true;

    private Path path;
    private int currentWaypoint = 0;
    RaycastHit2D isGrounded;
    Seeker seeker;
    Rigidbody2D rb;

    public void Start()
    {
    seeker = GetComponent<Seeker>();
    rb = GetComponent<Rigidbody2D>();

    InvokeRepeating("UpdatePath", 0f, pathUpdateSeconds);
    }

    private void FixedUpdate()
    {
    if (TargetInDistance() && followEnabled)
    {
    PathFollow();
    }
    }

    private void UpdatePath()
    {
    if (followEnabled && TargetInDistance() && seeker.IsDone())
    {
    seeker.StartPath(rb.position, target.position, OnPathComplete);
    }
    }

    private void PathFollow()
    {
    if (path == null)
    {
    return;
    }

    // Reached end of path
    if (currentWaypoint >= path.vectorPath.Count)
    {
    return;
    }

    // See if colliding with anything
    Vector3 startOffset = transform.position - new Vector3(0f, GetComponent<Collider2D>().bounds.extents.y + jumpCheckOffset);
    isGrounded = Physics2D.Raycast(startOffset, -Vector3.up, 0.05f);

    // Direction Calculation
    Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
    Vector2 force = direction * speed * Time.deltaTime;

    // Jump
    if (jumpEnabled && isGrounded)
    {
    if (direction.y > jumpNodeHeightRequirement)
    {
    rb.AddForce(Vector2.up * speed * jumpModifier);
    }
    }

    // Movement
    if (!isGrounded) force.y = 0;
    rb.AddForce(force);

    // Next Waypoint
    float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
    if (distance < nextWaypointDistance)
    {
    currentWaypoint++;
    }

    // Direction Graphics Handling
    if (directionLookEnabled)
    {
    if (rb.velocity.x > 0.05f)
    {
    transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    }
    else if (rb.velocity.x < -0.05f)
    {
    transform.localScale = new Vector3(-1f * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    }
    }
    }

    private bool TargetInDistance()
    {
    return Vector2.Distance(transform.position, target.transform.position) < activateDistance;
    }

    private void OnPathComplete(Path p)
    {
    if (!p.error)
    {
    path = p;
    currentWaypoint = 0;
    }
    }

    I would love to have an enemy whos following me and jumping ONLY when theres something blocking his way to me and not every time when I jump. Also when I will jump over him nearby he should jump too.
    If you will need anything more to see/know just text me and I will give you additional info.

    Thanks so much for your time.
    I will appreciate every help <3
     
  2. hms0589

    hms0589

    Joined:
    Jan 5, 2020
    Posts:
    27
    Code (CSharp):
    1.  
    2. Insert Code here
    3.  
    4. if(hungry)
    5. eat;
    6. else
    7. do something
    8.  
     
  3. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    Well, this didnt help me much :D