My 2d enemy is infinitley jumping
i did an advanced ai that followes me, everything works perfect but my 2d enemy can infinitly jump here is my code,i'm new to programming idk how to fix it, also i want to add an animation when my enemy walks, how do i do that? appreciate it! (sry for my bad english) using System.Collections; using System.Collections.Generic; using UnityEngine; using Pathfinding;
public class EnemyAI : MonoBehaviour { [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
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(-1f * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else if (rb.velocity.x < -0.05f)
{
transform.localScale = new Vector3(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;
}
}
}
Answer by junk_rat_angel · Aug 10, 2021 at 07:39 PM
I think the problem is outlined here https://answers.unity.com/questions/691622/why-does-raycast2d-always-return-non-null.html but basically I think for your IsGrounded variable you need to be storing what your raycast returns (which is not a boolean, except in the sense that it is not null so it will always return true which added to the fact that you never set jumpEnabled to false, means that that conditional will always return true) and then check the results for the null condition you want. Its a little confusing since you can do the implicit bool conversion described in the answer which doesnt really seem all that different from what your doing, but under the hood it is. It seems you can also possibly cast it to an int representing the number of hits detected and check that as well.
for the animations your gonna need to configure an animator component on the gameObject and then have something like:
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("1"))
{
anim.Play("cubeAnimation");
}
}
which is the top comment from this helpful little intro to the topic that you might wanna check out since unity has a whole special set of tools for working with animation https://www.youtube.com/watch?v=N73EWquTGSY
Your answer
Follow this Question
Related Questions
How to fix enemy's rotation (2D) 0 Answers
How to fix enemy's rotation (2D) 0 Answers
Tile-based Enemy AI issue 0 Answers
Specific 2D Shadowing Question 0 Answers
I can't do jump in my 2D game 1 Answer