How to move objects in Unity (3 methods)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
moving an object in unity can be relatively simple typically it involves changing the position value of an object's transform component either instantly or over time to create continuous movement however modifying an object's transform component isn't the only way to move a game object and depending on what it is you're trying to do a different method might be a better fit for your project in this video you'll learn three different methods for moving an object in unity how they're different and when to use one over another so you can choose the one that's right for your project so how do you move an object in unity the simplest way of moving an object in unity is by using its transform component which is a component that all game objects have and that determines the orientation and position of an object in your game's scene in a script you can access an object's own transform component using the transform property which is just transform written with a lowercase t you can then set the transform's position to move the object in the world either immediately by setting the position to a new vector3 value by adding a vector to it to shift the position of an object relative to its current position or by using transform translate which is a function of the transform class that will move an object by a certain amount every time it's called the translate function works in basically the same way as adding a vector to an object's position when it's called once it moves an object by a set distance which is defined by the vector3 value that's passed into the function when it's called every frame in update transform translate can be used to create continuous movement this works by calculating an object's movement vector on a per frame basis which typically involves multiplying three values together the direction of the movement as a unit vector a modifier to manually adjust the movement speed and time.deltatime which will convert the movement amount from units per frame to units per second a unit vector is like a regular vector except that its magnitude which is the length of the vector is always one unit this means that while a normal vector plots a movement over space using both length and direction a unit vector is just a definition of direction which is useful as it allows you to easily control the length of the vector with a float value in this case a speed modifier which when multiplied by time dot delta time which is how much time has passed since the last frame creates a movement value in units per second that can then be passed into the translate function but where does the direction vector come from unity provides a number of predefined values for common directional vectors such as forward up and right which can be used as ready-made unit vectors these are properties of the vector3 class they're basically just shorthand for standard direction values that save you having to declare a new vector 3 and type it out yourself for example vector 3 up is 0 1 0 vector 3 down is 0 minus 1 0 right is 1 0 0 and forward is 0 0 1 all in world space the transform class also provides a handful of direction vectors that are relative to the orientation of an object so for example transform.forward is still a vector of 0 0 1 except that it's forward along the object's blue z-axis instead of in world space which can be extremely useful for moving an object forwards relative to its own orientation instead of the world however keep in mind that some movement functions such as translate can operate in either world or local space which is the default meaning that if you use transform.forward with the default overload of transform translate you'll get a compounded effect where forward will appear to be in the wrong direction for this reason if you want to move an object in a relative direction when using transform translate be mindful of which space you're in either use vector3 direction values with local space which is the default behavior when using translate or set the space parameter of the translate function to world space and use local values instead while predefined directional vectors can be useful for basic movement to create a more complex movement that's based on player input it can be easier to simply pass the vertical and horizontal input axes straight into a new vector3 value and use that to create the vector3 direction to create eight-way movement for example however adding vectors together like this can cause a problem when moving an object using horizontal and vertical axes it's possible to create diagonal movement that is faster than the speed of movement in a single axis this is because the length of the vector that's created from a forward vector of 1 and a sideways vector of 1 is actually longer than either one on its own at around 1.4 which means that holding forward and right on a keyboard for example would move the player 1.4 times faster than just moving forwards or sideways causing uneven 8-way movement this is more commonly a problem with unity's input manager which separates the horizontal and vertical axes into individual values while unity's newer input system allows you to use a composite vector 2 that is normalized to 1 units to fix it either normalize the vector which will force it to a length of one or zero this is suitable for when you're using the raw input values and you want tight digital controls or clamp the magnitude of the vector to one which will retain analog precision but limit the length of the vector to one unit keeping it even in all directions which is useful for connecting a player's input directly to an object to make it move but what if you don't want the player to move in objects what if you want to move an object to a position or follow something else around generally speaking there are two ways to move an object to another position in unity by speed or by time move towards is a vector3 function that can be used to move an object towards a position at a specific speed and when it gets there it will stop without overshooting smooth damp works in a similar way to move towards except that the movement is eased creating smoother motion that speeds up and slows down on approach it also requires an external value identified using the ref keyword that the function will use to calculate the velocity of the object between frames both functions are controlled by their maximum speed which can be useful for creating dynamic movements where the target position may change before the object actually gets there such as having a camera follow a player for example however you may simply want to move an object to a known position over an exact period of time such as moving a platform or opening a door in which case you may find it easier to move the object using lerp lerp or linear interpolation can be used to change a value over a period of time between two known points it can be used to fade audio sources find values on a scale or move an object between two positions using vector3 lerp lerp works by returning a value between two others based on a third value t which is the position between the start and the end represented as a float between zero and one so for example if the start of the movement was at the origin and the end was 10 units up a t value of 0.5 would return a position in the middle at 5 units high by incrementing t which works by dividing the time elapsed during the movement by how long it's supposed to take it's possible to move an object over an exact period of time this typically works best in a co-routine where while the time elapsed is less than the duration the object is moved between the start and end positions after which it snapped into its final target position these methods work well for creating precise controlled movements but what if you don't want to control an object's movement precisely what if you'd rather push pull or throw an object creating movement using physical forces instead most rendered objects have some kind of collider component attached to them by default which gives them a physical presence in the world defined by the bounds of one or more colliders however an object with only a collider is considered to be a static object meaning that while it does have a physical presence it's not supposed to move to actually move an object under physics simulation you need to add a rigid body component to it a rigid body component is essentially a physics movement component it allows an object to be moved under physics simulation causing it to be affected by gravity other physics objects or moved by physical forces using the add force function add force allows you to apply a physical force to an object's rigid body either in a single burst using the impulse force mode or continuously building momentum and speed over time this works by passing in a vector3 force value which just like when moving an object using its transform can be modified using a unit vector and a variable float that allows you to control the amount of forces being applied however there are a couple of differences when moving an object using physics forces compared to moving an object with its transform for example when passing in a movement value to the addforce function you won't need to scale the movement amount by delta time the value pass in is a force in nuisance that's scaled automatically between physics calls meaning that the amount of movement that occurs each frame depends on a number of different factors such as the object's mass the amount of force applied and the time between updates which all takes place automatically as part of the function's calculations also because add force is a physics based function you'll need to apply it in fixed update not update for it to work correctly this is because fixed update is called in sync with unity's physics system which may be called more or less frequently than update which is called once per frame this is only necessary when applying force continuously which means that if you're using the impulse force mode to give an object a single push you don't necessarily need to use fixed updates to do it however if you're applying physics forces constantly doing it in fixed update is important for keeping changes in movement correctly in sync with the physics system what that does mean is that if you want to connect the add force function to the player's input which when using the input manager can only really be reliably detected in update you'll need to keep each function in their respective update calls and connect them using a value doing it this way means that the force calculations are correct and no input events are missed now i want to hear from you how are you moving objects in your game are you using physics are you moving objects using their transform components and what have you learned about moving objects in unity that you know others will find helpful whatever it is let me know by leaving a comment remember to like this video if you found it useful and get subscribed for more videos from me i'll see you next time [Music] you
Info
Channel: Game Dev Beginner
Views: 16,772
Rating: undefined out of 5
Keywords:
Id: JOiEz9fnc5Y
Channel Id: undefined
Length: 10min 52sec (652 seconds)
Published: Wed Jun 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.