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. Dismiss Notice

Blocking Swords with Maths

Discussion in 'Scripting' started by Cronnoc, Dec 3, 2020.

  1. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    18
    Hello, so I currently have a sufficient system which randomly generates sword swings for an AI to perform. My new goal is to get another AI to block these swings, with the intention of having two AI duel. Unfortunately, I have hit a roadblock that I have spent many hours trying to work out and failed. Thus, I come to you, members of Unity Forum, and ask for any aid you can provide.

    Assuming that I'm thinking correctly, all the AI should need to do is move its sword so that is perpendicular to the enemy sword (regardless of rotation) but always on the side that is closer to the AI. I'm sure this is pretty easy but maybe my brain isn't working because I cannot figure it out.

    Basically, the data I have to work with is both sword's Transforms.

    If you need more information (I feel I haven't provided enough) please let me know.

    Thank you in advance
     
  2. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    I always struggle with the maths but maybe some things to help:
    Could you try drawing a white board example in paint of how you're viewing the problem currently?
    How are you triggering that the second AI player should deflect at all?
    How would you translate one axis orientation to another axis orientation over time?
    How do you translate one point to another point in space over time?
    What is the orientation of both swords, and at what point do you want the second enemy to hit the first?
    How can the 2nd enemy predict into the future where that point should be? There will be a delay between seeing the swing and then wishing to deflect it once you initiate your swing, you don't want to lag behind your target.
     
  3. mellooow9

    mellooow9

    Joined:
    Dec 25, 2019
    Posts:
    31
    you could use transform.lookat() to make the sword "look" at the other sword. it always will point the z axis tho, but i think you could just rotate it 90 degrees or something in the right way to make it work with the axis you want.
     
  4. Cronnoc

    Cronnoc

    Joined:
    Jul 6, 2018
    Posts:
    18
    Sorry, I knew I didn't provide enough info. (I probably still won't unfortunately). I need my AI to calculate a position and rotation that will block the sword. Calculating where the enemy sword will be isn't to difficult so for the purposes of this calculation, imagine the sword is completely still. Here are some images that approximate what I need.
    block 1.PNG block 2.PNG Block 3.PNG

    Please help if you can, I have been trying to do this for far too many hours.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    To me it seems like this isn't actually a maths problem and you're potentially over complicating this it's more about how you detect the objects in the first place. What I would do is use multiple raycasts which is a common technique used with AI detecting what's in front of them. In this case I would potentially generate mulitple raycasts along the sword and have them detect both the sword and the player.

    This should then create a more dynamic sword fight as the AI struggle to try and find a gap in front of each other rather than have some boring pre-scripted sequence using maths calculations. It would be tricky to implement but I think it could work. A similar technique is used when dealing with traffic AI for example where you have cars that constantly read what's in front of them using raycasts and then they change direction accordingly.
     
  6. oldhighscore

    oldhighscore

    Joined:
    Nov 28, 2020
    Posts:
    79
    All you described is how to trigger the action, how does one make the swords move from the triggered events of the casts? "Change direction accordingly" -> "Swing sword accordingly", how would you write the code that swings accordingly?
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Well there's multiple ways to try this out, one way would be to have raycasts firing off from the actual body of the person. As an example just like in real life, if you have the opponent take a swing from the top down towards the head you then have a raycast pointing upwards from the head to detect the sword. If the top raycast is triggered you would then have an animation play where the person moves the sword to the appropriate position to block an attack from above.

    This is a more simplified version of it using keyframe animation but you could potentially make it more procedural I guess and have the sword move to block an attack from above by moving it to the correct position. What you're doing with these raycasts is your letting your fighters 'see' what's in front of them. This of course means though if you want to be more accurate you need to make more raycasts making this more expensive.

    The concept would be the same for the sides for example, randomly have the fighter play an attack animation to the side of the other fighter and have raycasts firing out from either side. If the sword is detected then the opponent can move their sword in to the side to block it. After that it's just a matter of delaying reaction time and so on to make it more realistic if the movement ends up being too 'perfect'.

    Again very similar concept to when you have moving vehicle traffic in a game, the raycasts will be constantly checking to see what's in front of them and help the vehicle react. I believe a similar method is used when it comes to dogfighting NPCs for example or even when they're just travelling through a 3D space and avoiding obstacles.
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Do you really have nothing but sword A and sword B .transform to work with?
    How do you calculate the attacker swing? you must have some way of calculating a block from it and sending it to the defender to act accordingly.
    show us some code, or atleast say what the swing functions are returning (relative positions, rotations, etc)


    if you only have the transforms to work with the only thing i can think of is tracking the attackers sword and from the path it draws try and predict what swing will happen, but that's not something you can do "by hand" imo, you're gonna have to train some AI for that S***, doesn't have to be all that complex so it's actually do-able, but let's stick with the top of my post first, haha.