How to Make a Basic but Effective Knife Melee Attack in a Snack Time

Поделиться
HTML-код
  • Опубликовано: 27 сен 2024
  • #FPSBuilders #UnityMeleeAttack #UnityTutorial
    ---Description--
    Hi there! Today I show you how to create a simple melee attack script and a damage script for a potential enemy. We will also create an animation for it so that it looks more natural. It's not too much complicated, but there're some steps to follow.
    Enjoy and please consider subscribing!
    Subscriber button taken here, if attribution was necessary: • FREE Animated RUclips ...
    ---Useful Links----
    --Video share link: • Make a Cool and Explos...
    -Subscribe: / @fpsbuilders4585
    ---About the channel---
    FPS Builders is a youtube channel focused on making fps tutorials for unity. If you're a dev and searching for some short and simple tutorials to make your own fps, this is the place fo you!
    Subscribe to be always up to date!
    / @fpsbuilders4585

Комментарии • 169

  • @isthisanewonlinegame
    @isthisanewonlinegame 2 года назад +15

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class EnemyDamage : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter(Collider other)
    {
    if (other.tag == "Melee")
    Destroy(gameObject);//Or apply a damage method
    }
    }

  • @KrangAnimations
    @KrangAnimations 2 года назад +3

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class meleeatack : MonoBehaviour
    {
    // vars
    Animator anim;
    private void Start()
    {
    anim = GetComponent();
    }

    private void Update()
    {
    if (Input.GetButtonDown("Fire1"))
    anim.SetBool("attacking", true);
    else if(Input.GetButtonUp("Fire1"))
    anim.SetBool("attacking", false);
    }
    }

  • @autumn_id
    @autumn_id 4 года назад +13

    Greetings from Russia! This was very helpful, good luck in your game development, you are doing really well! ;)

  • @aguy8007
    @aguy8007 2 года назад +1

    Bro, thanks for the animation code. Everywhere I looked, I couldn't find one that worked.

  • @vicren5075
    @vicren5075 3 года назад +1

    * tip if this doesn't work set the animator in the script to be public and drag the animator on the knife into the script to give the script a reference.

  • @PowerPlayerSam
    @PowerPlayerSam 6 месяцев назад

    Thanks for making my day amazing!

  • @calebcrabtree8549
    @calebcrabtree8549 3 года назад +2

    So you dont have to wait to the end to see the double knives you just duplicate the knife and put it onto the parrel side of the knife

  • @diogoschoeler2874
    @diogoschoeler2874 4 года назад +4

    in the conditions at the animator the options are: "blend" (in the place where it should be attacking) and "Greater" or "Less" where it should be "true" or "false". and it says tat parameter type attacking does not match, help pls
    Nvm i found the error

  • @konrad129
    @konrad129 9 месяцев назад

    4:40, what is that plug-in that formats line on placed semicolon?

  • @mmazuk1958
    @mmazuk1958 2 года назад

    Thanks for tutorial!

  • @jamesburt5385
    @jamesburt5385 4 года назад +1

    Oh god this helped

  • @unpopularsalad8153
    @unpopularsalad8153 4 года назад

    One day you will make a great fps game. Nice vid

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Thanks mate!

    • @sn0wyyy
      @sn0wyyy 4 года назад

      FPS Builders will one day be a creator
      he could one day be famous:D

  • @hshshshehd4
    @hshshshehd4 3 года назад +1

    my weapon goes back to where it was unless i hold i have been stuck here for so long

    • @hshshshehd4
      @hshshshehd4 3 года назад +1

      nvm i just had to put a exit time on the animator for the second animation
      thank you for this great tutorial!

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад +1

      You're welcome!

  • @teepot9541
    @teepot9541 4 года назад +2

    Can you give me the script in the reply's please

  • @southsidebrickrigsrpserver2120
    @southsidebrickrigsrpserver2120 4 года назад +2

    When I click the button to attack nothing happens plz help

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Make sure each part of the script is correct

    • @southsidebrickrigsrpserver2120
      @southsidebrickrigsrpserver2120 4 года назад

      FPS Builders it was, I found out I had to turn off has exit time on the default animation to get it to work

  • @HappyGamer263
    @HappyGamer263 10 месяцев назад

    and it goes to third person ???

  • @drip_sand
    @drip_sand 5 месяцев назад

    Legend

  • @aweseome245
    @aweseome245 4 года назад +1

    I fixed it, and i made the enemydamage script but now im getting Assets\EnemyDamage.cs(21,12): error CS0103: The name 'otherTag' does not exist in the current context
    and Assets\EnemyDamage.cs(21,12): error CS0103: The name 'Enemy' does not exist in the current context
    how do i fix

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад +1

      Maybe you forgot to create the tag 'Enemy'

    • @MukeshKumar-fo1dn
      @MukeshKumar-fo1dn 4 года назад +1

      Yup you forgot to do that

    • @aweseome245
      @aweseome245 4 года назад +1

      @@fpsbuilders4585 i did that and it didn't work, same errors

    • @kindafunnymonkey
      @kindafunnymonkey 2 года назад

      I am very late but it should be other.tag I think. You might also wanna check capitalisation across every time you wrote enemy.

  • @Jax_Augustin
    @Jax_Augustin 3 года назад

    hey I'm new at coding and I cant figure out what word he put above "private void Start()"

  • @mikethegamedev
    @mikethegamedev 3 года назад

    Thank you so much!

  • @atomicalex1732
    @atomicalex1732 4 года назад +1

    cool, but how would i be able to make something like punching/kicking?
    where it would not destroy but instead knock them back?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад +1

      It can be interesting to do a video about it

    • @manlast643
      @manlast643 3 года назад

      Simply add a rigidbody component to the object your knife collides with and then add a force to that rigidbody.
      Here is a link to the documentation on addforce: docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    • @atomicalex1732
      @atomicalex1732 3 года назад

      @@manlast643 thanks

  • @fungben6668
    @fungben6668 3 года назад

    Excuse me, can you give me the Discord's link ? After done the code , i cant use the melee attack also the animations is not playing , when i click the "Has Exit Time" then it(animations) start play but i can't control it , i need so help over here

  • @fatrickwashere3072
    @fatrickwashere3072 3 года назад

    Hello there is a problem even if i hit the enemy it doesnt destroy it

  • @rosscurry3862
    @rosscurry3862 3 года назад +2

    Amazing tutorial thanks so much! Also I love the music and would really appreciate a link if you still have it!!! Would love to listen to this song while I code

  • @shnerped3549
    @shnerped3549 4 года назад +2

    The enemy won't get destroyed when I hit it! How do I fix this?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад +1

      Hello. Did you assign the tag to the Enemy?

    • @Goodgis
      @Goodgis 4 года назад +4

      The Knife needs to have a rigid body. Just had the same problem!

    • @curiousman4452
      @curiousman4452 4 года назад +2

      Make sure you checked "Is Trigger" from your weapons collider.

    • @lukasvolcik5109
      @lukasvolcik5109 3 года назад

      @@Goodgis Holy shit thanks man :D

  • @KrangAnimations
    @KrangAnimations 2 года назад

    tysm! look out 4 stick smack on steam!

  • @kulot2212
    @kulot2212 9 месяцев назад

    can you put the code please?

  • @かまぬ-x4q
    @かまぬ-x4q 3 года назад

    I need help. The knife phases through any other object except the enemy how do i fix?

  • @Rals3i
    @Rals3i 3 года назад +1

    Great video, but I keep getting the error "Input button fire 1 is not setup" how do i fix this.

    • @buildev22
      @buildev22 3 года назад

      If you get this error than you want to got to edit > project settings > input manager, after getting there look to see if a "Fire 1" button is configured. If not then change the size of the input manager by one and create one yourself. Hope this helped :)

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад +1

      As Elliot said

  • @rategg3791
    @rategg3791 3 года назад

    pls i do not have Animator at the code

  • @PoweringBacon
    @PoweringBacon 4 года назад +2

    Didnt work
    Nvm i didnt add that false part

    • @lukasvolcik5109
      @lukasvolcik5109 3 года назад

      As someone helped me, the attacking object needs a rigid body. It starts to detect the right collisions with it.

  • @jackbudge2440
    @jackbudge2440 3 года назад

    Im running into the problem I have no errors popping up so I know its not code and well I've double checked and its the same but when I attack the enemy nothing happens it doesn't destroy him
    Help?

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад

      Probably something with the animator

    • @jackbudge2440
      @jackbudge2440 3 года назад

      Sorry should have deleted the comment I had to add a rigid body and it worked thanks anyway!

  • @reiimouse
    @reiimouse 3 года назад

    When I try to run it,it says "ArgumentException: Input Button Firel is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    MeleeAttack.Update () (at Assets/Project/Scripts/MeleeAttack.cs:19)"
    Help

  • @NotTheBeb
    @NotTheBeb 3 года назад

    Great video! could you supply a download link for this?

  • @EthanWuProductivity
    @EthanWuProductivity 4 года назад

    Plz help! I keep geting stuck at the animator part. When i try to set a condition it just says Parameter does not exist in controller. Any way to fix this?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Watch your paramters in the animator tab

    • @hshshshehd4
      @hshshshehd4 3 года назад

      create a parameter in the parameter tab called atack

  • @friesandcats1919
    @friesandcats1919 3 года назад

    how do i open up the animater window? not the animation window, the Animater

    • @f1l1pqk30
      @f1l1pqk30 3 года назад

      i have the same problem

    • @friesandcats1919
      @friesandcats1919 3 года назад

      @@f1l1pqk30 ok so when you create an animation it should also creat like this weird box in one corner another box diagonal from it and jsut double click that and it should work fine :)

  • @macksbocklage
    @macksbocklage 2 года назад

    3:43

  • @fajans4606
    @fajans4606 4 года назад

    nice vid indeed. but how do you connect the "entry" to "none"?? what button did you pressed?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Create an empty state with no animation on it and connect entry with it

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Right click to connect and select make transition

    • @fajans4606
      @fajans4606 4 года назад

      @@fpsbuilders4585 thanks

  • @yoyu7685
    @yoyu7685 4 года назад

    why my conditions is Blend and not attacking?

  • @aweseome245
    @aweseome245 4 года назад

    parameter 'attacking' does not exist
    I made the parameter and added it to the script and everything, is there a fix?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Make sure that you write it right in the script. Otherwise it should work

    • @aweseome245
      @aweseome245 4 года назад

      @@fpsbuilders4585 I copied it letter for letter

    • @aweseome245
      @aweseome245 4 года назад

      @@fpsbuilders4585 I fixed it, and i made the enemydamage script but now im getting Assets\EnemyDamage.cs(21,12): error CS0103: The name 'otherTag' does not exist in the current context
      and Assets\EnemyDamage.cs(21,12): error CS0103: The name 'Enemy' does not exist in the current context
      how do i fix

  • @a_exi7285
    @a_exi7285 2 года назад

    3:42 how did you connected them?

  • @bryanwangfilms
    @bryanwangfilms 3 года назад

    Thanks it works great but how can you give the enemy's health.

    • @friesandcats1919
      @friesandcats1919 3 года назад

      basically set up the script for the enemy, and give him a public float "health" and give it a set length, and have it so each hit makes the health go away by 1 instead of destroying the enemy, and destroy the enemy after the health reaches zero

    • @bryanwangfilms
      @bryanwangfilms 3 года назад

      @@friesandcats1919 Thx!

    • @friesandcats1919
      @friesandcats1919 3 года назад

      @@bryanwangfilms yo I am apparently having trouble with this myself, y’a know how to make the health go away only when the animation is playing? And only by one at a time?

    • @ex0rion408
      @ex0rion408 3 года назад

      @@friesandcats1919 the way I did it is that I put the box collider at the top of whatever weapon you are using PS: this really only works with longer tools

  • @CooperES
    @CooperES 4 года назад

    Mine is looping and it's sooooo annoying pls help

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Remember to stop the animation in the script

  • @gavindavis2180
    @gavindavis2180 4 года назад

    My weapon is not returning to its original position

  • @daeimo3751
    @daeimo3751 4 года назад

    How do i make this after i pick up a weapon?(not starting in the hand)

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      I think you just need to add the scritp to the knife and when you take it, you can active it

    • @daeimo3751
      @daeimo3751 4 года назад

      @@fpsbuilders4585 Done, now it works, how do i make the animation not repeat after clicking once? and having to press click over again to attack?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      The script in the video should do it. Make sure you call anim.SetBool("attacking",false) and check your animator also

  • @Sharkz-iz6yf
    @Sharkz-iz6yf 4 года назад

    I can swing my sword, but I don’t kill the wnemy

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Remeber to add the tag melee to the knife

    • @Sharkz-iz6yf
      @Sharkz-iz6yf 4 года назад

      @@fpsbuilders4585 i did that

    • @Sharkz-iz6yf
      @Sharkz-iz6yf 4 года назад

      @@fpsbuilders4585 didnt work

    • @v4lk394
      @v4lk394 4 года назад +2

      @@Sharkz-iz6yf add an Rigidbody to the knive That worked for me

    • @carlabalos3884
      @carlabalos3884 3 года назад +1

      @@v4lk394 Yeah good thing you point that out. No collision will be detect unless one has a rigidbody is attached to any of those game objects.

  • @alecelliott7506
    @alecelliott7506 2 года назад +2

    I'm getting the error "Parameter 'attacking' does not exist" when I hit play the animation I made loops infinity and nothing happens when I click.

  • @feeney8715
    @feeney8715 2 года назад +1

    not very good tutorial, i dont recommend this tutorial if you dont know about the animator that much like me, all he shows is "click click thats done okay click done okay done"

  • @mintyunbox5307
    @mintyunbox5307 2 года назад +1

    i came here to see how to swing and its what i got, thank you.

  • @sausageswiper2397
    @sausageswiper2397 4 года назад +15

    Your weapon works fine, but the only issue I see is that the weapon will destroy anything it touches without a mouse press (unless I did it wrong, which is a possibility). I would suggest turning off the collider upon ButtonUp and turning it on ButtonDown.

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад +3

      Thanks for the adivce! You can also add an if specifing which tag should be hit by the weapon

    • @Celcious
      @Celcious 3 года назад +4

      add "if (collision.collider.tag == "Your enemy tag")
      {
      Destroy(collision.gameObject);
      }"

  • @Rxce2k
    @Rxce2k 4 года назад +2

    i have to hold down left click for a second in order for my knife to hit is there a way to make it less long of a click?

    • @atomicalex1732
      @atomicalex1732 4 года назад +1

      you know the thing that has attacking false in the animator? just set that to have exit time

    • @Rxce2k
      @Rxce2k 4 года назад +1

      Atomic Alex thanks bro

  • @DavidPatMathis81
    @DavidPatMathis81 Год назад

    *plays video*
    *techno blaring*
    *stops video*
    Nope

  • @kamuisw
    @kamuisw 3 года назад +1

    help!!! so the animation of the knife is always acctivated. how do i fix it?

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад +1

      Remember to stop it with anim.SetBool(attacking , false)

    • @kamuisw
      @kamuisw 3 года назад

      @@fpsbuilders4585 Thanks! it fix it! Really good tutorial

  • @tanmanlol2732
    @tanmanlol2732 4 года назад +3

    Thanks!

  • @heehee6514
    @heehee6514 2 года назад +1

    How do i delete the transition??

  • @VaydenMan
    @VaydenMan 2 года назад

    The Slicing Doesnt work for me. The animation wont play

  • @frosty7652
    @frosty7652 Год назад

    how do i make the None state orange at 3:42

  • @anchzeror
    @anchzeror 4 года назад +2

    0:40 song name??

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад +1

      It's a song from youtube audio, now I don't remember the name sorry :(

    • @anchzeror
      @anchzeror 4 года назад +1

      @@fpsbuilders4585 found it -
      ruclips.net/video/NWubBrMWCRM/видео.html

  • @JesseTuovinen
    @JesseTuovinen 4 года назад +1

    My animation loops even when i have the loop time off, can someone help?

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Make sure to stop it in the script

    • @JesseTuovinen
      @JesseTuovinen 4 года назад

      @@fpsbuilders4585 It wasn't the script, but i got it working!

    • @jujuthethird4602
      @jujuthethird4602 3 года назад

      @@JesseTuovinen how did you fix it

    • @nickablocka8822
      @nickablocka8822 3 года назад

      @@JesseTuovinen how?

    • @JesseTuovinen
      @JesseTuovinen 3 года назад

      @@nickablocka8822 My animation was so short that I had to make it slower in the animator window

  • @rategg3791
    @rategg3791 3 года назад

    it does not work the other do not light when i put the tag near it

  • @v4lk394
    @v4lk394 4 года назад

    I always get this error: NullReferenceException: Object reference not set to an instance of an object
    HammerSwing.Update () and the swing animation dont start playing

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      You probably forgot to assign an object to the script or in the script itself

    • @v4lk394
      @v4lk394 4 года назад

      @@fpsbuilders4585 I fixed it. I made an public gameobj and draged the animator in

  • @a_protato8702
    @a_protato8702 2 года назад

    Nice tutorial bro first one that worked

  • @raphaelchenevert5536
    @raphaelchenevert5536 4 года назад

    HELP HELP !!!!!!!!! im getting en error at line 20 what the f!!!!!! is it
    here it is :
    anim.SetBool("attacking", true) PLeASe HEWLp

    • @fpsbuilders4585
      @fpsbuilders4585  4 года назад

      Hello there! If you can write the error it gives you, it can help more. Maybe you didn't create the animation or something like that

    • @TheAmazing-Earth
      @TheAmazing-Earth 3 года назад

      anim.SetBool("attacking", true);

  • @MuhammadRiaz-nr3qj
    @MuhammadRiaz-nr3qj 3 года назад

    i made an animation that was roughly 1 second long. when i quick press left click, it only plays through half of the animation before snapping back to its resting position. is there a way for it to go through the whole animation before going back to its resting position? btw, this video was really helpful

    • @siggimatti1129
      @siggimatti1129 2 года назад +4

      I know this is late but this could help someone. You just need to check "Has exit time" on the transition from the attack animation to the none state.

  • @runox5185
    @runox5185 3 года назад

    Your so underrated man nice tutorial, simple and quick

  • @noneyrin4387
    @noneyrin4387 3 года назад

    Hey just want to know the background music btw best channel for shooting games😙

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад +1

      Thanks! I'm sorry but I don't remember, it's a music youtube gives to make videos

    • @noneyrin4387
      @noneyrin4387 3 года назад

      @@fpsbuilders4585 it's okay😶😙

  • @ownerthegamer1218
    @ownerthegamer1218 3 года назад

    What is the apply damage method?

  • @enginejoopie4201
    @enginejoopie4201 3 года назад

    what is a tag?

  • @pumo8511
    @pumo8511 3 года назад

    hey i cant hit with thr sword how should i repair that??

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад

      It doesn't kill the enemies? Make sure they have the tag

    • @pumo8511
      @pumo8511 3 года назад

      @FPS Builder yes they have my sword doesent wanna moove

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад

      @@pumo8511 Try checking the code

  • @OL-jk6uv
    @OL-jk6uv 3 года назад

    How come my animation isnt playing?

    • @fpsbuilders4585
      @fpsbuilders4585  3 года назад

      Go to my Discord for help

    • @OL-jk6uv
      @OL-jk6uv 3 года назад

      @@fpsbuilders4585 i did but no one is responding