Unity - Character Controller Moveable Object

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to show you how you can use a character controller to be able to push object around your scene just like as you see right here all right let's get it [Music] what is up YouTube It's Leighton coming at you again with the online code coach and in this video I got my buddy here Jacob what's up Jacob yes and you listen I'm doing all right so we're here to fix yet another problem that many folks might be having in here in unity whenever you're using a player with a character controller you find it kind of difficult when it comes to pushing objects around in your scene that has rigid body and you should be able to push them around so I'll demonstrate here real quick this uh player here that we're using has a character controller and you can see it doesn't always work sometimes it moves so we're I'm just trying to push this and this doesn't always work but you can see here the consistency if I was to use um a character controller here or a different character here that does not use a character controller camera and this there we go so with this one now we can easily just push the object it's a little bit more consistent see let's go ahead and address the problem you can see here with the character controller you you sometimes will get it to maybe work a little bit you see I'm trying to just push these boxes and it's it's really buggy it's not really set up to work um even though all those boxes have rigid body on them you can see the the plate themselves using the character controller only has a cap controller and a script so it doesn't really have like a collider and even if you do add maybe a box collider to this [Music] um the way it's moving still doesn't really allow it to work consistently you can see I'm trying to push push this here yeah still like it will go every now and then but that um that doesn't work so if you have a regular character or humanoid obviously you won't be adding a box glider so I'm not going to do that either but I was just an example enough with the uh explanation of what's wrong let's get on with fixing it I'm going to show you a quick little script that you can drop on every object you want any object you want to be able to add a a movability to interact with the character controller um you can do that and then it will be able to you'll be able to push it and move it around let's go ahead and create a new script what shall we call it Jacob [Music] uh maybe movable objects oh Jack I like it movable object that is the immunity a lot of time here to do its little point point point one thinking there all right and we're up we're in the script let me move my face out the way we'll just grip over to the left a little bit easier for everyone to read all right first thing we're going to create is a float we're going to create a float uh let's make it public and this float will be basically the magnitude the force off the the response to the push so um that's actually not a bad name I'll call it push um Force all right I'll also also just set it to one by default so I just put a one there that means that in unity will just show up as a one but you can change it anytime you want we won't need the update function we won't need the start function either so you can go ahead and clear those just like the on trigger enter and the on trigger exit or the on collision enter on collision exit there is a detection for when the character controller hits something and that one is actually called on um on controller collider hit so you can go ahead and auto generate that function if you have to have things set up properly you can use that is that your brother in the background yeah what's his name Daniel what's up Daniel what's up Daniel all right awesome so there is this on controller collider hit that will detect when an object is um interacting with uh an object with a controlled on it or capture controller in this thing so you can detect when the cap controller actually hits something um we're gonna use this to be able to push some stuff around but of course you can also use this for some other times in your in your um game when Maybe uh trigger isn't working the on controller collider hit should work all right so hit is going to represent the um the object that it just hit so we're gonna try to detect if that object has a rigid body so we're gonna say we're gonna create ourselves a reference to the ridge about it so let's create our type here where's your body I'm just going to call mine underscore rig uh yeah underscore Rich I'll just do two two e's and then we're going to say that is equal to whatever we hit dot um you can say rigid body here but that's not the one you're looking for you want to first get into the Collider and then do attached rigid body do this one right just do what semicolon at the end and you're set so now you've accurately um grabbed or snatched the rigid body from what I really just hit if it has one so we want to make sure we do that check though want to check to make sure that the ridge body is not equal no which means there actually is a rigid body for us to do what we're about to do once there is a rigid body we can then start calculating some Force so let's go ahead and determine where the the push is coming from so we can make it move in the opposite direction so cradle Vector three and this will be our um let's just say it's our Force Direction so we'll say Direction and that is going to be equal to the hit not game object so the game object that we're hitting we're gonna go into the transform of that game object and find the position all right so we got that position now we want to minus it from our current position the current position of the object that the script is on that will give us our Force Direction right so um this is the object we're pushing this is the little guy pushing it I don't know why his hands is coming out of his um his his head there let's say he's pushing it so we need his position right and then the object's position so we're taking his position first boom and minus in the position of this so we can get basically the Direction Boom of where um the the push the force should be coming from okay cool beans let us continue next we want to make sure that our Force position doesn't start um doesn't include the up and down right we just want x and z we don't want y so we go into the Y and we will just confirm that that is zero just go ahead and hard code it to zero so we go into this three Vector X Y and Z and per personally go ahead and Target the Y and set it to zero so it remains zero it doesn't change after that we just want to normalize this so we say Force Direction Dot normalize and that requires a little bit of a open and close parenthesis and then the semicolon at the end of what I do wrong here I think there's a capital N no ah normal lies without the D there we go that's the word once that is set we are good to go all we need to do now is use this rigid body and add force in the correct direction which we have right there we've required all Direction everything is ready to go so now it's time to add Force so let's hop into the rigid body that we just created that we just made reference to bridge and then we're going to say add Force at position that's a different one you can see here it applies four is out of position as well as a result of this will apply uh torque and force on an object so this is perfect uh in case you guys haven't noticed there is a lot of stuff you get a lot of different forces you can use here there's explosion Force there's add torque there's a lot of different ways to apply friction and movement to an object so we're going to go ahead and use this add Force at position it requires an open and closed parentheses and in here you can see first thing we need is a force Vector 3 direction for force and a vector 3 for the position all right we already know um the force amount will be this and then it's a vector 3 Force so we named this along with it so to tell it which direction that force is coming from so we force we say force force Direction and simply times it by the push four so now we have a force Direction a vector 3 with a force so that is the first thing it requires so you can see the little pop up here first thing requires second thing now is now the position which we also have so we'll put a comma space and then the position guys is simply from the position of the object again this object that the script is on um we're not done we can add a little bit more there's a little override we can do here but we can close up the script and call it a day um however I find that for the best result you can still do an override so in case you guys have never noticed there's a not override but overload uh usually the intellisense will sometimes show you right here where it says one overload which means you can do a little bit more than what is normal so if you just put a comma usually you'll be able to see what the next thing is and it's a Force Mode just like when you add force or add exposure Force they all have this ability here where you can tell it how the force is being added there's like um constant force there is impulse and stuff like that and speaking of impulse that's actually exactly what we're going to do so we're going to say Force mode and then we're just going to say dot impulse so it's a little bit faster all right so I know that was kind of long and drawn out but I want to make sure that everyone understands what we're doing um and that's the code if we just detect the collision with the um character controller um try to snatch the rigid body off the object we're hidden make a reference I should say but not like actually snatch and then check make sure it's there calculate which direction the push should be coming from and then we go ahead and add it all right so now that we're done with the script let's go ahead and just hit save and now you'll be able to just drop this on the oh I did say the object that you want to push um that's my fault that's my fault you're actually going to put it on the player himself with his character controller right and boom just like so because I mean it just makes sense if the hit is the other object that has the rigid body then obviously the other object wouldn't be the player with the character controller because player does not have rigid body so everything else that has a rigid body should be good to go and go ahead and you see the force there is set to one usually that works fine for me but put it on our object on our characters now when we hit play everything that has a ridge body we should be able to just push them around nice and easily just like that you can see it works nice and accurately if you want to be super strong you can go ahead and crank this up let's try five and you see you get a lot more push power that was crazy look at that so yeah using character controller guys that's how you're able to push things around in your scene feel free to copy the script use a script for yourself I'll leave a link in the description or you can go ahead and just copy it over if you like the video it was helpful go ahead and drop a like and say hi to Jacob of course you got to say hi to Jacob all right and that's it we are out and make sure to leave a sub and a sub Jacob says a sub anything else Jacob all right deuces
Info
Channel: Online Code Coaching
Views: 7,761
Rating: undefined out of 5
Keywords: Unity, How to, Character controller, puch object, moving object, move, puch, with character controller, push, moving object with, character, character controller move object, force, rigidbody, push rigidbody
Id: 4g483XSnuoA
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Thu Jul 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.