Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I Create an Enemy Lunge Attack, While The Enemy Has an AI Agent?

Discussion in 'Scripting' started by MackEZ_1, Mar 1, 2023.

  1. MackEZ_1

    MackEZ_1

    Joined:
    Sep 4, 2021
    Posts:
    10
    Hey all, I'm working on a topdown shooting game and I wanted to create an enemy attack that makes the enemy leap towards the player direction after a short charge up animation. Right now I'm using an AI Agent to move the enemy towards the player. When the enemy gets close enough to the player I'd like them to stop moving, start charging, and then lunge forward where the player was standing.

    The main issue I'm having is that I want a controllable lunge distance that's consistent no matter how far the player is. I've tried doing this with an AddForce using the Impulse ForceMode but it's very inconsistent and physics based, and when I use the MoveTowards or Lerp function to move them they either teleport or will change in speed based on how far the player is from them when they begin the lunge.

    Example of what I'm looking for would be something like these guys from Hades.
    upload_2023-3-1_10-47-23.png upload_2023-3-1_10-47-39.png upload_2023-3-1_10-47-52.png

    What would be the best way to do this? Please Obi-Wan, you're my only hope!
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    If you are using physics based controllers than all such a movements are going to be very difficult and error prone.

    I see you are starting this "game business" so I'm suggest using kinematic controllers.
     
  3. MackEZ_1

    MackEZ_1

    Joined:
    Sep 4, 2021
    Posts:
    10
    It is Kinematic now, I wrote off trying to use physics for it.
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    What you are telling us is not enough, you would have to show us some code.
     
  5. MackEZ_1

    MackEZ_1

    Joined:
    Sep 4, 2021
    Posts:
    10
    here's the script I have for the enemy, sorry about that.
     

    Attached Files:

  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Paste here on the forum your plunge attack code.
     
  7. MackEZ_1

    MackEZ_1

    Joined:
    Sep 4, 2021
    Posts:
    10
    The enemy controller script is all the code I have on the enemy right now, I was thinking of using an Enumerator to work out the attack but haven't implemented anything yet
     
  8. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    You need to separate things in your code, right now I see a mess.
    Decompose your code, and you will probably find problem yourself.
     
  9. MackEZ_1

    MackEZ_1

    Joined:
    Sep 4, 2021
    Posts:
    10
    I'm not sure what you mean by decompose my code? It's all separated and readable on my end.
     
  10. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Ok paste here the code that is not working and should work, but without other things like "OnDeath" "Start" etc.