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
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
}
}
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);
}
}
Greetings from Russia! This was very helpful, good luck in your game development, you are doing really well! ;)
Thank you! Really appreciated :)
@Jokubas Remeika Greetings man!
Bro, thanks for the animation code. Everywhere I looked, I couldn't find one that worked.
* 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.
Thanks for helping the community
Thanks for making my day amazing!
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
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
Good job!
4:40, what is that plug-in that formats line on placed semicolon?
Thanks for tutorial!
Oh god this helped
Glad to help!
One day you will make a great fps game. Nice vid
Thanks mate!
FPS Builders will one day be a creator
he could one day be famous:D
my weapon goes back to where it was unless i hold i have been stuck here for so long
nvm i just had to put a exit time on the animator for the second animation
thank you for this great tutorial!
You're welcome!
Can you give me the script in the reply's please
When I click the button to attack nothing happens plz help
Make sure each part of the script is correct
FPS Builders it was, I found out I had to turn off has exit time on the default animation to get it to work
and it goes to third person ???
Legend
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
Maybe you forgot to create the tag 'Enemy'
Yup you forgot to do that
@@fpsbuilders4585 i did that and it didn't work, same errors
I am very late but it should be other.tag I think. You might also wanna check capitalisation across every time you wrote enemy.
hey I'm new at coding and I cant figure out what word he put above "private void Start()"
Thank you so much!
You're welcome!
cool, but how would i be able to make something like punching/kicking?
where it would not destroy but instead knock them back?
It can be interesting to do a video about it
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
@@manlast643 thanks
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
Hello there is a problem even if i hit the enemy it doesnt destroy it
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
The enemy won't get destroyed when I hit it! How do I fix this?
Hello. Did you assign the tag to the Enemy?
The Knife needs to have a rigid body. Just had the same problem!
Make sure you checked "Is Trigger" from your weapons collider.
@@Goodgis Holy shit thanks man :D
tysm! look out 4 stick smack on steam!
can you put the code please?
I need help. The knife phases through any other object except the enemy how do i fix?
Does it have a collider?
Great video, but I keep getting the error "Input button fire 1 is not setup" how do i fix this.
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 :)
As Elliot said
pls i do not have Animator at the code
Didnt work
Nvm i didnt add that false part
As someone helped me, the attacking object needs a rigid body. It starts to detect the right collisions with it.
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?
Probably something with the animator
Sorry should have deleted the comment I had to add a rigid body and it worked thanks anyway!
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
Fire1 not FireI
@@fpsbuilders4585 Ok,thank you ^^
Great video! could you supply a download link for this?
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?
Watch your paramters in the animator tab
create a parameter in the parameter tab called atack
how do i open up the animater window? not the animation window, the Animater
i have the same problem
@@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 :)
3:43
nice vid indeed. but how do you connect the "entry" to "none"?? what button did you pressed?
Create an empty state with no animation on it and connect entry with it
Right click to connect and select make transition
@@fpsbuilders4585 thanks
why my conditions is Blend and not attacking?
You have to change its name in the animator
parameter 'attacking' does not exist
I made the parameter and added it to the script and everything, is there a fix?
Make sure that you write it right in the script. Otherwise it should work
@@fpsbuilders4585 I copied it letter for letter
@@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
3:42 how did you connected them?
right click
Thanks it works great but how can you give the enemy's health.
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
@@friesandcats1919 Thx!
@@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?
@@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
Mine is looping and it's sooooo annoying pls help
Remember to stop the animation in the script
My weapon is not returning to its original position
Probably you didn't stop the animation
@@fpsbuilders4585 how do I fix it?
anim.setBool("name of animation",false)
How do i make this after i pick up a weapon?(not starting in the hand)
I think you just need to add the scritp to the knife and when you take it, you can active it
@@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?
The script in the video should do it. Make sure you call anim.SetBool("attacking",false) and check your animator also
I can swing my sword, but I don’t kill the wnemy
Remeber to add the tag melee to the knife
@@fpsbuilders4585 i did that
@@fpsbuilders4585 didnt work
@@Sharkz-iz6yf add an Rigidbody to the knive That worked for me
@@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.
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.
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"
i came here to see how to swing and its what i got, thank you.
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.
Thanks for the adivce! You can also add an if specifing which tag should be hit by the weapon
add "if (collision.collider.tag == "Your enemy tag")
{
Destroy(collision.gameObject);
}"
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?
you know the thing that has attacking false in the animator? just set that to have exit time
Atomic Alex thanks bro
*plays video*
*techno blaring*
*stops video*
Nope
help!!! so the animation of the knife is always acctivated. how do i fix it?
Remember to stop it with anim.SetBool(attacking , false)
@@fpsbuilders4585 Thanks! it fix it! Really good tutorial
Thanks!
How do i delete the transition??
The Slicing Doesnt work for me. The animation wont play
how do i make the None state orange at 3:42
nvm i figured it out
0:40 song name??
It's a song from youtube audio, now I don't remember the name sorry :(
@@fpsbuilders4585 found it -
ruclips.net/video/NWubBrMWCRM/видео.html
My animation loops even when i have the loop time off, can someone help?
Make sure to stop it in the script
@@fpsbuilders4585 It wasn't the script, but i got it working!
@@JesseTuovinen how did you fix it
@@JesseTuovinen how?
@@nickablocka8822 My animation was so short that I had to make it slower in the animator window
it does not work the other do not light when i put the tag near it
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
You probably forgot to assign an object to the script or in the script itself
@@fpsbuilders4585 I fixed it. I made an public gameobj and draged the animator in
Nice tutorial bro first one that worked
HELP HELP !!!!!!!!! im getting en error at line 20 what the f!!!!!! is it
here it is :
anim.SetBool("attacking", true) PLeASe HEWLp
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
anim.SetBool("attacking", true);
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
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.
Your so underrated man nice tutorial, simple and quick
Hey just want to know the background music btw best channel for shooting games😙
Thanks! I'm sorry but I don't remember, it's a music youtube gives to make videos
@@fpsbuilders4585 it's okay😶😙
What is the apply damage method?
what is a tag?
hey i cant hit with thr sword how should i repair that??
It doesn't kill the enemies? Make sure they have the tag
@FPS Builder yes they have my sword doesent wanna moove
@@pumo8511 Try checking the code
How come my animation isnt playing?
Go to my Discord for help
@@fpsbuilders4585 i did but no one is responding