Тёмный

5. Knockback Effect--Just Add Enemies! (Unity Tutorial) 

Night Run Studio
Подписаться 4,4 тыс.
Просмотров 17 тыс.
50% 1

Make enemy encounters feel more genuine by adding a knockback effect that sends the character backward on impact. This tutorial will break down the steps needed to create the effect, and leave you with the ability to fine-tune the effect for the needs of your project.

Игры

Опубликовано:

 

27 мар 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 122   
@navzy6211
@navzy6211 2 года назад
only guide that isn't 30 minutes and worked, underrated af, subscribed hope you do great
@NightRunStudio
@NightRunStudio 2 года назад
Thanks so much for the encouragement! More videos to come.
@ins3rt915
@ins3rt915 Год назад
This tutorial is just what I needed, and the fact that you responded to basically every comment is awesome too
@NightRunStudio
@NightRunStudio Год назад
Thanks! I really love hearing when these vids are helpful to people. Cheers!
@aidynoconnor582
@aidynoconnor582 2 года назад
Awesome guide. Easy to follow, and unlike most of the other knockback tutorials, easy to slot this one into an existing bit of script. Thank you for the tutorial :)
@NightRunStudio
@NightRunStudio 2 года назад
Thanks for the encouragement! It’s so good to hear that this style of tutorial is helpful for people.
@Adamseslar
@Adamseslar Год назад
Awesome guide I really appreciate how easy to follow this was. One change I made was instead of having the object causing the knock back always having hold of your movement script I just added a knock back function inside the player script which would set the KBcount=KBtotal and could be triggered from the object on collision and return if the other object didn't have the tag of player.
@NightRunStudio
@NightRunStudio Год назад
Thanks for the kind words! I like the idea of causing the in KB in the player script. One of the reasons for moving it to the enemy’s was just to keep the player controller from getting too crowded (I teach high school students and crowded scripts can seem overwhelming). That said, I love your solution. It does simplify what the system has to go through. Thanks for sharing the idea!
@lightcred8758
@lightcred8758 Год назад
thank you very much for this tutorial, so clear!
@NightRunStudio
@NightRunStudio Год назад
Thanks! So glad to hear you found it helpful.
@nathansoares3050
@nathansoares3050 Год назад
Thanks man, great tutorial!
@NightRunStudio
@NightRunStudio Год назад
Glad it helped!
@Jossiz
@Jossiz Год назад
Thanks dude it's working perfectly.
@NightRunStudio
@NightRunStudio Год назад
Sweet! Glad to hear it was helpful.
@Colecia
@Colecia Год назад
This tutorial series has been absolutely great so far! I do have a slight issue though, the script seems to work, but when I'm testing it, the game can't decide whether or not he's getting hit from the right or the left. It will set KnockFromRight to true no matter what side I'm hit on.
@NightRunStudio
@NightRunStudio Год назад
Glad you’re enjoying the series! The problem you’re reporting is usually an issue of a misplaced ! Mark or a backwards < sign. If you double check the logic for getting your knock from right bool, it sounds like the knock from right = false conditions are never being met. I would start there. The other possibility is that you are not getting an accurate FacingRight value in the first place. A good way to check that is with a debug.log check that tells you the value of facingRight each time you turn. I hope that helps!
@Colecia
@Colecia Год назад
@Night Run Studio Thank you for the help! What I ended up doing was defining a new bool to determine if I was on the left or right instead of checking for it in the knockback if statement.
@NightRunStudio
@NightRunStudio Год назад
@@Colecia Awesome! glad to hear you got it working. And thanks for sharing your workaround. Good luck with the rest of your project.
@bullfangorex787
@bullfangorex787 Год назад
Clear and work fine. Thank you.
@NightRunStudio
@NightRunStudio Год назад
Awesome! Glad to hear everything is working for your project.
@bullfangorex787
@bullfangorex787 Год назад
@@NightRunStudio Just one thing, when my player get kb, he can move again durring the fall, he don't have to get back to the ground. Any idea how i can make him can't move untill he's landing?
@NightRunStudio
@NightRunStudio Год назад
@@bullfangorex787 This should actually be quite easy to accomplish. At the moment, you should have the line that allows you to move tucked into an if statement that checks if your KBCounter is down to 0. Mine looks like this: if(KBCounter
@bullfangorex787
@bullfangorex787 Год назад
@@NightRunStudio seems logical, I have a isGrounded too. I'll try this. Thank you for your time and help.
@NightRunStudio
@NightRunStudio Год назад
@@bullfangorex787 My pleasure. Hopefully everything gets working as intended. Cheers!
@nieeel676
@nieeel676 9 месяцев назад
So you make the player check to hit the enemy from the right or left through the script in the enemy. Can you do the same thing in the script player? because if I have a lot of enemies I have to drag & drop the player reference to the inspector because it won't be saved in the enemy prefab
@NightRunStudio
@NightRunStudio 8 месяцев назад
You could totally set this up in the player if you prefer. You could also code your enemies the find the player without needing to drag and drop. This would just mean that in your Start() method you would have something like: Player = GameObject.Find(“NameOfYourPlayer”);
@benjaminrodriguezcruz6212
@benjaminrodriguezcruz6212 2 месяца назад
Hi Tutorial worked great thak you so much! However when it bounces from the enemy it goes towards the correct direction but it shoots up. Any suggestion or tips? Thank you!!
@NightRunStudio
@NightRunStudio 2 месяца назад
If you don't want it to bounce up at all, did you try just changing the y coordinate to a 0?
@_WaterFruit
@_WaterFruit Месяц назад
Is there a way for the player to smoothly move on the x when they are hit instead of just stopping as if they hit a wall?
@NightRunStudio
@NightRunStudio Месяц назад
At the moment, when the KBCounter ends, we turn control back over to the inputs (so no input = no velocity). You can definitely get more precise, but the code gets trickier then. For example, you could add a grounding requirement (if (KBCounter
@user-xl1sr4rn8i
@user-xl1sr4rn8i 11 месяцев назад
Very helpful, but I ran into a small problem. It works for enemies but the effect is not the same when colliding with projectiles or moving objects. Player still takes damage but the knockback effect is not working. What should I do?
@NightRunStudio
@NightRunStudio 11 месяцев назад
That’s pretty odd, as the code should work for any object-including projectiles. It’s hard to know what’s up without more detail. Is there any movement at all? It might be helpful if you posted a video or shared your code. Feel free to pop into my discord if you want and we can take a closer look. You can find me here: discord.gg/UM8kqeyd
@NoobyMain
@NoobyMain Год назад
hello! uh so like, when the player touches the character, they VERY SLOWLY bounce off, but then literally force themselves to glue to the enemy and i barely can control the player, how can i fix it?
@NightRunStudio
@NightRunStudio Год назад
Oh dear, that is a very strange result. It sounds like you player's transform is somehow being set equal to your enemy's (or something like that). There is nothing in this code that I can see doing that, but... strange things happen sometimes :) If you'd like to paste your code here, I'd be happy to take a look. The other option would be to remove one line of code at a time (from the new stuff you've added), until you find the offending line of code.
@asketime2280
@asketime2280 6 месяцев назад
Hi! I have a question that somebody might be able to help. I followed the instructions you give in this video, but for some reason, when the enemy touches the player, instead of the player going backwards it is going upwards and because of that, it kills the enemy. What should I do? Thanks in advance
@NightRunStudio
@NightRunStudio 5 месяцев назад
That sounds like an error in the angle of the knockback. Have you double checked the vector2 to make sure it that you have a value on the x axis and not just the y axis?
@asketime2280
@asketime2280 5 месяцев назад
Thanks for the quick response! It finally worked! I made the script a hundred times and it finally worked! I believe that the curve made by the knockback was too high and the enemy (coming towards the player) was being hit by accident on their weak point. I changed some parameters such as gravity and the KBForce and now it seems not to hit the enemy by mistake. I can say that this is my first week in Unity and coding, so, everything I have learnt is from your videos and other youtuber. Looking forward to watching more of your videos!😄 @@NightRunStudio
@lightofficial7321
@lightofficial7321 10 месяцев назад
hello, I am having an issue where my KnockBackCounter goes down to something like 0.15 and it doesnt go to 0 ): any ideas why?
@NightRunStudio
@NightRunStudio 10 месяцев назад
That's a weird one... if you want to share your code (or pop into my discord channel, I could take a look and see if we can figure it out). discord.gg/er6WseAF
@rs-bu1tw
@rs-bu1tw Год назад
some time the damage happens multiple time i have two colliders idid what you .collider rather than .game objects it didnt work the 2 colliders are in the same game objects which is the player both are in the parent any help?
@NightRunStudio
@NightRunStudio Год назад
This really comes down to design. The nature of OnCollisionEnter is that it will fire for every collider involved. So, if you have 2 colliders on the enemy and 2 on the player, you get it firing 4 times. There are a couple of ways to solve this... 1) using children. If you move extra colliders onto child objects, and use the .collider command (like you mentioned above), you can get the object to ignore extra collisions and only focus on the parent collision. 2) Another option is to use Triggers. If you really need to have other colliders on your object, and don't want to add children, you can make this script work with a trigger instead. You will have to change your method to OnTriggerEnter2D, and make sure to check trigger on the collider. Now, it should only fire once (since you only have the one trigger). I hope that helps!
@rs-bu1tw
@rs-bu1tw Год назад
@@NightRunStudio the second solution will work for me i guess thanks i Appreciate you for freeing time for me and others
@kyleparrella7378
@kyleparrella7378 5 месяцев назад
Hello, great video! For some reason, when my player collides with the enemy the player is only knocked straight up. I have tried multiple solutions like setting the vector 2 x to rb.velocity = new Vector2(kbForce * 1000, 0.2f * kbForce); to try and giving the x more power but the result is the same. Would you have any idea why?
@NightRunStudio
@NightRunStudio 5 месяцев назад
That's a great question. I often find that the problem is that some other script is modifying your velocity. For example, in this script, we make sure that your movement is turned off while the KBCounter is > 0 because if we didn't, that movement line would override our attempts to knockback the player (since both are directly assigning a velocity). Is there anywhere else in your game that sets the player's velocity? If so, you may have to deactivate that during knockback.
@RichieDev
@RichieDev 3 месяца назад
that is probably beacuese you located the code in Update() function instead of FixedUpdate() function
@No_body8157
@No_body8157 2 месяца назад
@@RichieDev what if the code for the knockback is in the FixedUpdate() function but I am still going straight up? Any thoughts on how to help?
@RichieDev
@RichieDev 2 месяца назад
@@No_body8157 check your Rigidbody, is it dynamic?
@henrikasvaranauskas7508
@henrikasvaranauskas7508 Год назад
i doubt that you will see this but, i am having issues, the knockback doesnt actually work, i have 2 colliders, a box and a circle one on my enemies, it doesnt seem like there are any issues with the script and i am not getting any errors but my character doesnt get knocked back. EDIT: nvm got it working, turns out i put the counter to 0.2 instead of the Total time, my bad whoops. cool guide, works great now, thanks
@NightRunStudio
@NightRunStudio Год назад
Awesome! So glad to hear that you figured it out. Also, thanks for sharing your fix--it might help someone else down the road.
@M-vd9ii
@M-vd9ii 5 месяцев назад
Hey I followed this video and the knockback works but Im using a movement script that has wall jumping in it and whenever I wall jump it now does the knockback thing instead of doing the wall jump. Wondering if you could help me. Thanks
@NightRunStudio
@NightRunStudio 5 месяцев назад
That's pretty odd. Unless, of course, you accidentally put the enemy's damage script on your tilemap or something. It only triggers if an object with this script collides with the player, so it really shouldn't happen when wall jumping. One test would be to put a debug on your playerMovement script that fires whenever knockbackCounter > 0... then you can find out when it is being triggered... Another thought would be to disable all enemies in your scene and see if the problem is still happening when wall jumping... then slowly add back objects until you find which one is causing the problem.
@M-vd9ii
@M-vd9ii 5 месяцев назад
Thanks for the help. I just fixed it by added to the if statement in my playermovement script where it does if knockfromright = true/false i added && !iswalljumping so it cant happen when im walljumping.@@NightRunStudio
@NightRunStudio
@NightRunStudio 5 месяцев назад
Glad you figured it out!@@M-vd9ii
@abaier82
@abaier82 Год назад
When I put in "playermovement" into the enemy damage script, it says their is an error. I spell it correctly but it doesn't work. What should I do?
@NightRunStudio
@NightRunStudio Год назад
Can you paste your code here and then I’ll take a look.
@abaier82
@abaier82 Год назад
@@NightRunStudio Keep in mind that my movement script isnt the same as yours. I also finished the whole script even with the error Movement: using UnityEngine; namespace Assets { public class PlayerMovement : MonoBehaviour { public float speed; private float Move; public float jump; public bool isJumping; public float KBforce; public float KBcounter; public float KBtotalTime; public bool knockFromRight; private Rigidbody2D rb; // Start is called before the first frame update void Start() { rb = GetComponent(); } // Update is called once per frame void Update() { if (KBcounter
@abaier82
@abaier82 Год назад
@@NightRunStudio I also know that it is probably the movement script, because doing the same thing works with other scripts, but not the movement
@NightRunStudio
@NightRunStudio Год назад
Hmmmn… I’m pretty sure you’re right. Now, I haven’t explored the use of namespaces a lot, but I think the namespace on your movement script could be causing the problem. If we’re lucky, you might be able to solve this simply by adding Using Assets; At the top of you Enemy script. This should give the scripts access to the Assets namespace, allowing the scripts to talk to each other. Hopefully that helps!
@No_body8157
@No_body8157 2 месяца назад
I have followed this tutorial but I am just getting a knockback straight up instead of actually getting a knockback. Would it be possible to get some help with this issue?
@NightRunStudio
@NightRunStudio 2 месяца назад
It's hard to say without knowing more, but this is usually a result of either: a) an error in the knockbackangle (just double check that you have a horizontal value in your Vector); or, b) something else is controlling you x velocity in another script (usually somewhere in your movement script). Is there something else that sets your velocity? (especially something that gives it a 0?) if neither of those checks out, feel free to pop into the discord server and share your code.
@No_body8157
@No_body8157 2 месяца назад
@@NightRunStudio I am pretty sure it has something to do with my wallslide/walljump script or even my dash script. But I don't know how to fix it. The reason I think it is those two things is because my player no longer "jumps" away from the wall just jumps straight up. I would love to join your discord but I can't get any of the links in this comments thread to work to join.
@NightRunStudio
@NightRunStudio 2 месяца назад
@@No_body8157 That sounds like it is probably what's happening. The downside of creating characters and piecing together different scripts is that they sometimes conflict--but we all run into that as we learn. You can try out this link: discord.gg/YXcAKc7N You'll probably want to share the knockback script as well as the one for your wallslide. Cheers.
@JohnnyBriones
@JohnnyBriones Год назад
Okay the damage went well! I am here now haha Is there an easy way to apply this to all 4 directions and not just left and right? Thanks!
@NightRunStudio
@NightRunStudio Год назад
Awesome! So glad things are working. As to the 4-direction question, I'm just going to share what I mentioned to another person who had the same question: The main difference between this and a topdown is in calculating the direction of the knockback. I intended to make a video for this eventually but haven't gotten to it yet. The main way to do this would be something like Vector2 difference = collision.transform.position - transform.position; //this figures out your direction difference = difference.normalized * kBforce; //normalized changes the vectors to have a magnitude of 1 (e.g., 1, 1; or 1, 0) AddForce(difference, ForceMode2D.Impulse); //adds your force in the direction you want. Mister Taft Creates does something really similar to this in his tutorial, which you might find useful: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-QnsGSCXknUY.html Hope that helps!
@JohnnyBriones
@JohnnyBriones Год назад
@@NightRunStudio You are the man! I was thinking it would need the Vector2. I did find his video and got knockback to kind of work. Well I think it is working find but the issue I have is each time I attack I lose health. In you other video with the character health, this is what I have: public class mobDamage : MonoBehaviour { public int damage; public playerHealth playerhealth; private void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "Player") { playerhealth.TakeDamage(damage); } } } The parent I have is Player with the tag player. Then the children are the hitboxes for the sword attack. Does this mean the sword attacks have the tag of Player since it is the child of a parent with Player? How would I adjust this to fix the attacking hurts me as well. Thanks!
@NightRunStudio
@NightRunStudio Год назад
@@JohnnyBriones This is one of those cases where the other fix I mentioned should help. I think you are right that the enemy thinks the sword is the player, and is causing damage because of it. Try changing: if(collision.gameObject.tag == "Player") to: if(collision.collider.gameObject.tag == "Player") Than the enemy should only look at the sword's collider.
@Pavetl
@Pavetl Год назад
How can I make it work for a Top Down game?
@NightRunStudio
@NightRunStudio Год назад
The main difference between this and a topdown is in calculating the direction of the knockback. I intended to make a video for this eventually but haven't gotten to it yet. The main way to do this would be something like Vector2 difference = collision.transform.position - transform.position; //this figures out your direction difference = difference.normalized * kBforce; //normalized changes the vectors to have a magnitude of 1 (e.g., 1, 1; or 1, 0) AddForce(difference, ForceMode2D.Impulse); //adds your force in the direction you want. Mister Taft Creates does something really similar to this in his tutorial, which you might find useful: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-QnsGSCXknUY.html Hope that helps!
@wadetwynham4734
@wadetwynham4734 11 месяцев назад
Hi, @ 3:35 where you add 0.2 and the player gets pushed back, mine pushes my player up. Not back
@NightRunStudio
@NightRunStudio 11 месяцев назад
Well that's annoying! :) That wont be a result of the KB counter (that just allows the knockback to happen). If you are going up instead of back, then the force is applied on the y axis instead of the x. If you go back to 3:10 and double check that all of your values match up... I have a feeling that is where the error is coming from. Good luck!
@wadetwynham4734
@wadetwynham4734 11 месяцев назад
That's what I thought as well but my values are the same, it's completely weird anyway thanks....I love your content
@NightRunStudio
@NightRunStudio 11 месяцев назад
Thanks for that @wadetwynham4734 ! One other thought: does your player get pushed up no matter what side he gets hit from? or have you only tested it with being hit from one direction? (if it is happening on both sides, then the error is probably somewhere else, but if it's only on one side, then it's definitely something to do with your KBForce values).
@KMM277
@KMM277 10 месяцев назад
@@NightRunStudio Hi I have the same problem the KBForce just works on the y axis the x just doesnt move
@NightRunStudio
@NightRunStudio 10 месяцев назад
@@KMM277 That's a weird one, and it's hard to trouble shoot without more information (I haven't come across that problem myself). That said, feel free to pop into my discord server and share your code. Maybe we can figure it out from there. discord.gg/CbWc3JBy
@muhammadalimadriansyah5247
@muhammadalimadriansyah5247 2 года назад
why my KBcounter on my player script always go down until it minus?
@muhammadalimadriansyah5247
@muhammadalimadriansyah5247 2 года назад
ohh never mind :D btw thanks for your video bro
@NightRunStudio
@NightRunStudio 2 года назад
Glad you figured it out. Cheers!
@derechtemidnaplays
@derechtemidnaplays Год назад
Hey i´ve got a little problem with the knockback. First of I use a completely different movement script. The knockback is working with it but the problem is that my character gets hit twice. Can you help?
@NightRunStudio
@NightRunStudio Год назад
The most common reason for this problem is if you have more than one collider on your player (then it hits multiple times). One workaround is to change: if(collision.gameObject.tag... to if(collision.collider.... adding the ".collider" should cause the script to only look at the single collider, instead of all the colliders on the player.
@derechtemidnaplays
@derechtemidnaplays Год назад
@@NightRunStudio My player Only has one collider. So that can't be the problem
@NightRunStudio
@NightRunStudio Год назад
@@derechtemidnaplays I could be wrong, but if you have more than one gameObject (like a parent object with a child sprite, for example), that could also be causing it to fire multiple times. I would try adding the .collider line just in case. Let me know if that doesn't work.
@derechtemidnaplays
@derechtemidnaplays Год назад
@@NightRunStudio Was a little bit lazy. So what do you mean with "..."?
@NightRunStudio
@NightRunStudio Год назад
@@derechtemidnaplays currently your script should read something like: if(collision.gameObject.tag == "Enemy") (You may have a different tag name), but to keep it from firing multiple times, you may need to change it to: if(collision.collider.gameObject.tag == "Enemy) That should cause it to only consider the game object attached to the collider, rather than all game objects connected to the player.
@dorner12
@dorner12 11 месяцев назад
RESPECT
@notpeanutyt
@notpeanutyt Год назад
where is the script for the player movement? also great job on these tutorials.
@NightRunStudio
@NightRunStudio Год назад
You can find my playerMovement tutorial and script here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Ov40_jAKrMk.html And, thanks for the encouragement!
@notpeanutyt
@notpeanutyt Год назад
@@NightRunStudio thank you i actually didnt think you would respond you earned +1 sub for that
@NightRunStudio
@NightRunStudio Год назад
@@notpeanutyt Thanks for that! I love interacting with people who are using these vids, so feel free to fire future questions or feedback my way. Cheers!
@zoroff5675
@zoroff5675 7 месяцев назад
I do with You Everything Please i need the Script plzzz
@JohnnyBriones
@JohnnyBriones Год назад
I am Back haha I created some code that works when the player hits the enemy and the enemy gets knocked back. I am trying to add knockback on the character so when he get hits, he get's knocked back. I went in and switch the tag to "Player" But it is not working. It seemed like an easy script to make fore the enemies. Any ideas why it might not be working? Here is the code [SerializeField] private float thrust; private void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.gameObject.CompareTag("Player")) { Rigidbody2D character = collision.gameObject.GetComponent(); if (character != null) { StartCoroutine(KnockCoroutine(character)); } } } private IEnumerator KnockCoroutine(Rigidbody2D character) { Vector2 forceDirection = character.transform.position - transform.position; Vector2 force = forceDirection.normalized * thrust; character.velocity = force; yield return new WaitForSeconds(.3f); character.velocity = new Vector2(); }
@NightRunStudio
@NightRunStudio Год назад
Hmmn... what's it doing at this point when you plug this into your project? Is there any reaction when you collide with the enemy?
@JohnnyBriones
@JohnnyBriones Год назад
@@NightRunStudio the enemy hits me and I stay still. no movement.
@NightRunStudio
@NightRunStudio Год назад
@@JohnnyBriones At a glimpse, I'm not seeing where the problem is. I think the key here is for you to run some debug logs. If you're not familiar with them, you can simply insert Debug.Log("test1"); after a line of code, if it prints to your console while running the game, then you know that the code made it that far without encountering an error. When I have an issue like this I will insert several (test 1, 2, 3, etc). If I see all of them except for "test 3" in my code, then I know that I have found where my error is. For example, I would put one right at the start of your IEnumerator coroutine... that way you will know if the code is even trying to fire that code, or if the error is happening sooner.
@JohnnyBriones
@JohnnyBriones Год назад
@@NightRunStudio That is a fantastic idea! I will do that from now on. Thanks! I placed the code where you said and the debug ran. I put it at the very end of the code block and it still ran. I do have a value for the thrust value too so not sure why it is not working
@NightRunStudio
@NightRunStudio Год назад
@@JohnnyBriones Well, at least you know all of the code is running! Two thoughts: 1) try running a debug to make sure that it is applying the force to your character. For this, I would add the following code at the end of your coroutine: Debug.Log(character.name); If you see your player's name, then you'll know it has the right target. 2) Have you tried making your force VERY large? Perhaps it is applying force, but it's just not doing anything because it is not enough force.
Далее
Unity Tutorial: Knockback anyone in ANY game
27:20
Просмотров 9 тыс.
Best exercises to lose weight ! 😱
00:19
Просмотров 4,3 млн
A new way to generate worlds (stitched WFC)
10:51
Просмотров 519 тыс.
Tierlisting the BEST (and worst) GAME ENGINES
33:51
Просмотров 210 тыс.
Simple Wave spawner in Unity 2D
35:08
Просмотров 27 тыс.
How To DAMAGE Enemies in Unity
9:19
Просмотров 43 тыс.
How to Use Actions in Unity [Unity Tutorial]
14:49
Просмотров 31 тыс.
AI Learns How To Play Doodle Jump
12:02
Просмотров 18 тыс.
How to Code (almost) Any Feature
9:48
Просмотров 669 тыс.