Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ben2618 · Aug 09, 2021 at 04:47 PM · 2d2d gameenemy ai

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;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

343 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges