How to move object in unity

How to move object in unity

Position GameObjects

Alternatively, you can select each of the five Transform modes from the Scene A Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary view’s Tools Overlay or with a hotkey:

At the center of the Move Gizmo, there are three small squares you can use to drag the GameObject within a single plane (meaning you can move two axes at once while the third keeps still).

If you hold shift while clicking and dragging in the center of the Move Gizmo, the center of the Gizmo changes to a flat square. The flat square indicates that you can move the GameObject around on a plane relative to the direction the Scene view An interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary Camera A component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary is facing.

Rotate

With the Rotate tool selected, change the GameObject’s rotation by clicking and dragging the axes of the wireframe sphere Gizmo that appears around it. As with the Move Gizmo, the last axis you changed will be colored yellow. Think of the red, green and blue circles as performing rotation around the red, green and blue axes that appear in the Move mode (red is the x-axis, green in the y-axis, and blue is the z-axis). Finally, use the outermost circle to rotate the GameObject around the Scene view z-axis. Think of this as rotating in screen space.

Scale

The Scale tool lets you rescale the GameObject evenly on all axes at once by clicking and dragging on the cube at the center of the Gizmo. You can also scale the axes individually, but you should take care if you do this when there are child GameObjects, because the effect can look quite strange.

RectTransform

The RectTransform is commonly used for positioning 2D elements such as Sprites A 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary or UI elements, but it can also be useful for manipulating 3D GameObjects. It combines moving, scaling and rotation into a single Gizmo:

Note that in 2D mode, you can’t change the z-axis in the Scene using the Gizmos. However, it is useful for certain scripting techniques to use the z-axis for other purposes, so you can still set the z-axis using the Transform component in the Inspector.

Transform

The Transform tool combines the Move, Rotate and Scale tools. Its Gizmo provides handles for movement and rotation. When the Tool Handle Rotation is set to Local (see below), the Transform tool also provides handles for scaling the selected GameObject.

Custom tools

If your Project uses custom Editor tools, some of them might also allow you to position GameObjects.

You can access custom tools from the Editor Tools dropdown menu in the Scene view Tools toolbar A row of buttons and basic controls at the top of the Unity Editor that allows you to interact with the Editor in various ways (e.g. scaling, translation). More info
See in Glossary Overlay.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

For information, see the documentation on Using Custom Editor Tools.

Gizmo handle position toggles

The Gizmo handle position toggles found in the Tool Settings Overlay are used to define the location of any Transform tool Gizmo, and the handles used to manipulate the Gizmo itself.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

For position

Use the dropdown menu to switch between Pivot and Center.

For rotation

Use the dropdown menu to switch between Local and Global.

Snapping

Unity provides three types of snapping:

Surface snapping

While dragging in the center using the Move tool, hold Shift and Control (Command on Mac) to quickly snap the GameObject to the intersection of any Collider.

Vertex snapping

Use vertex snapping to quickly assemble your Scenes: take any vertex from a given Mesh and place that vertex in the same position as any vertex from any other Mesh you choose. For example, use vertex snapping to align road sections precisely in a racing game, or to position power-up items at the vertices of a Mesh.

Follow the steps below to use vertex snapping:

Select the Mesh you want to manipulate and make sure the Move tool is active.

Press and hold the V key to activate the vertex snapping mode.

Move your cursor over the vertex on your Mesh that you want to use as the pivot point.

Hold down the left mouse button once your cursor is over the vertex you want and drag your Mesh next to any other vertex on another Mesh.

To snap a vertex to a surface on another Mesh, add and hold down the Shift+Ctrl (Windows) or Shift+Command (macOS) key while you move over the surface you want to snap to.

To snap the pivot to a vertex on another Mesh, add and hold the Ctrl (Windows) or Command (macOS) key while you move the cursor to the vertex you want to snap to.

Release the mouse button and the V key when you are happy with the results (Shift+V acts as a toggle of this functionality).

Look-at rotation

While using the Rotate tool, hold Shift and Control (Command on Mac) to rotate the GameObject towards a point on the surface of any Collider.

Screen Space Transform

While using the Transform tool, hold down the Shift key to enable Screen Space mode. This mode allows you to move, rotate and scale GameObjects as they appear on the screen, rather than in the Scene.

Positioning GameObjects

When building your games, you’ll place lots of different objects in your game world.

Translate, Rotate, and Scale

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

At the center of the Translate gizmo, there are three small squares that can be used to drag the object within a single plane (i.e. two axes can be moved at once while the third is kept still). If you have a three button mouse, you can click the middle button and drag to adjust the axis that was most recently moved (the arrow for this axis will change to yellow). If you hold shift while clicking and dragging in the center of the Translate Gizmo, the centre of the gizmo changes to a flat square, indicating that you can move the object around on a plane relative to the way the Scene View camera is facing.

With the rotation tool selected, you can change the object’s rotation by clicking and dragging the axes of the wireframe sphere gizmo that appears around it. As with the translation tool, the last axis you changed will be colored yellow and can be further adjusted by clicking the middle mouse button and dragging. Think of the red, green and blue circles as performing rotation around the red, green and blue axes that appear in the translate mode. (Red is the x-axis, green in the y-axis, and blue is the z-axis.) Finally, the outer-most circle can be used to rotate the object around the scene view z-axis. You can think of this as rotating in screen space.

The scaling tool lets you rescale the object evenly on all axes at once by clicking and dragging on the cube at the center of the gizmo. You can also scale the axes individually, but you should take care if you do this when there are child objects since the effect can look quite strange. Again, the last axis changed appears in yellow and can be adjusted by dragging with the middle mouse button.

Note that in 2D mode, the Z axis can’t be changed in the scene using the gizmos. However, it is useful for certain scripting techniques to use the Z axis for other purposes; you can still set the Z axis from the inspector in such cases.

For more information on transforming GameObjects, please view the Transform Component page.

Gizmo Display Toggles

The Gizmo Display Toggles are used to define the location of any Transform Gizmo.

Unit Snapping

While dragging any Gizmo Axis using the Translate Tool, you can hold the Control key (Command on Mac) to snap to increments defined in the Snap Settings.

You can change the unit distance that is used for the unit snapping using the menu Edit->Snap Settings…

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unityScene View Unit Snapping settings.

Surface Snapping

While dragging in the center using the Translate Tool, you can hold Shift and Control (Command on Mac) to snap the object to the intersection of any Collider. This makes precise positioning of objects incredibly fast.

Look-At Rotation

While using the Rotate Tool, you can hold Shift and Control (Command on Mac) to rotate the object towards a point on the surface of any Collider. This makes orientation of objects relative to one another simple.

Vertex Snapping

You can assemble your worlds more easily with a feature called Vertex Snapping. This feature is a really simple but powerful tool in Unity. It lets you take any vertex from a given mesh and with your mouse place that vertex in the same position as any vertex from any other mesh you choose.

With this you can assemble your worlds really fast. For example, you could align road sections precisely in a racing game or position power up items at the vertices of a mesh.

Using vertex snapping in Unity is simple. Just follow these steps:

Positioning GameObjects

To select a GameObject, click on it in the Scene view or click its name in the Hierarchy window. To select or de-select multiple GameObjects, hold the Shift key while clicking, or drag a rectangle around multiple GameObjects to select them.

Selected GameObjects are highlighted in the Scene view. By default, this highlight is an orange outline around the GameObject; to change the highlight color and style, go to Unity > Preferences > Color and edit the Selected Wireframe and Seleted Outline colours. See documentation on the Gizmo Menu for more information about the outline and wireframe selection visualizations. The selected GameObjects also display a Gizmo in the Scene view if you have one of the four Transform tools selected:

Move, Rotate, Scale, and RectTransform

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

Use the four Transform tools in the toolbar to Move, Rotate, Scale, or Rect Transform individual GameObjects. Each has a corresponding Gizmo that appears around the selected GameObject in the Scene view. To alter the Transform component of the GameObject, use the mouse to manipulate any Gizmo axis, or type values directly into the number fields of the Transform component in the Inspector.

Alternatively, you can select each of the four Transform modes with a hotkey: W for Move, E for Rotate, R for Scale and T for RectTransform.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unityThe Move, Scale, Rotate, and Rect Transform Gizmos

At the center of the Move Gizmo, there are three small squares you can use to drag the GameObject within a single plane (meaning you can move two axes at once while the third keeps still). If you have a three-button mouse, click the middle mouse button and drag to adjust the axis that was most recently moved (the arrow for this axis will change to yellow).

If you hold shift while clicking and dragging in the center of the Move Gizmo, the center of the Gizmo changes to a flat square. The flat square indicates that you can move the GameObject around on a plane relative to the direction the Scene view Camera is facing.

Rotate

With the Rotate tool selected, change the GameObject’s rotation by clicking and dragging the axes of the wireframe sphere Gizmo that appears around it. As with the Move Gizmo, the last axis you changed will be colored yellow; adjust this further by clicking the middle mouse button and dragging. Think of the red, green and blue circles as performing rotation around the red, green and blue axes that appear in the Move mode (red is the x-axis, green in the y-axis, and blue is the z-axis). Finally, use the outermost circle to rotate the GameObject around the Scene view z-axis. Think of this as rotating in screen space.

Scale

The Scale tool lets you rescale the GameObject evenly on all axes at once by clicking and dragging on the cube at the center of the Gizmo. You can also scale the axes individually, but you should take care if you do this when there are child GameObjects, because the effect can look quite strange. Again, the last axis changed appears in yellow and can be adjusted by dragging with the middle mouse button.

RectTransform

The RectTransform is commonly used for positioning 2D elements such as Sprites or UI elements, but it can also be useful for manipulating 3D GameObjects. It combines moving, scaling and rotation into a single Gizmo:

Note that in 2D mode, you can’t change the z-axis in the Scene using the Gizmos. However, it is useful for certain scripting techniques to use the z-axis for other purposes, so you can still set the z-axis using the Transform component in the Inspector.

For more information on transforming GameObjects, see documentation on the Transform Component.

Gizmo handle position toggles

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The Gizmo handle position toggles are used to define the location of any Transform tool Gizmo, and the handles use to manipulate the Gizmo itself.

For position

Click the Pivot/Center button on the left to toggle between Pivot and Center.

For rotation

Click the Local/Global button on the right to toggle between Local and Global.

Unit snapping

While dragging any Gizmo Axis using the Move tool, hold the Control key (Command on Mac) to snap to increments defined in the Snap Settings (menu: Edit > Snap Settings…)

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

Surface snapping

While dragging in the center using the Move tool, hold Shift and Control (Command on Mac) to quickly snap the GameObject to the intersection of any Collider.

Look-at rotation

While using the Rotate tool, hold Shift and Control (Command on Mac) to rotate the GameObject towards a point on the surface of any Collider.

Vertex snapping

Use vertex snapping to quickly assemble your Scenes: take any vertex from a given Mesh and place that vertex in the same position as any vertex from any other Mesh you choose. For example, use vertex snapping to align road sections precisely in a racing game, or to position power-up items at the vertices of a Mesh.

Follow the steps below to use vertex snapping:

Select the Mesh you want to manipulate and make sure the Move tool is active.

Press and hold the V key to activate the vertex snapping mode.

Move your cursor over the vertex on your Mesh that you want to use as the pivot point.

Hold down the left mouse button once your cursor is over the vertex you want and drag your Mesh next to any other vertex on another Mesh.

Release the mouse button and the V key when you are happy with the results (Shift+V acts as a toggle of this functionality).

Note: You can snap vertex to vertex, vertex to surface, and pivot to vertex.

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.

Copyright © 2017 Unity Technologies. Publication: 5.6-001N. Built: 2017-07-12.

How to Move Objects in Unity

There’s many ways to move objects in Unity and this guide explains how and when each option is best used.

This guide focuses on setting object positions. Rotation and scale are only briefly mentioned but still give a basic overview allowing you to manipulate them too!

Transform Component

All game objects in Unity have a transform component. This is used to store the position, rotation and scale of your object. The transform can be read to get this information, or can be set which will change the position, rotation or scale of the game object in the scene.

You can interact with a transform component in a script by either directly assigning a Transform to a variable or if you have a Game Object variable you can access the transform component with myObject.transform.

Setting the position of a transform overview

Once you have a variable referencing the target transform there’s a few ways you can move it. These methods are explained in more detail below!

Directly moving a transform to a world position

The world position is a location in the scene which is always the same, no matter where your gameobjects are or the depth of your objects. For example X:0, Y:0, Z:0 will always be the same position in the center of the world.

To set the world position you use the position property and simply set it to a vector3 type.

transform.position = new Vector3(120f, 50f, 45f);

This example will instantly move your object to X:120, Y:50 and Z:45 in world space. It sets the position of the transform to a new Vector3 variable (which is a variable type which stores X, Y and Z coordinates used for positioning in 3D space) and we’re initializing the variable with the defined X, Y and Z values.

If you’re wanting to slowly move objects in unity into a position rather than instantly teleport then check out this guide which explains lerping and interpolation in more detail! https://www.unity3dtips.com/unity-fix-movement-stutter/

Directly moving a transform to a local position

Before explaining more about local positions you need to understand how depth, parents and children work in Unity. The local position is a position which is different depending on the location of the parents. (which allows you to do movement within a separate space, such as moving an object inside a moving spaceship without recalculating position based on the spaceship position)

To set the local position of an object similarly to setting the world position you assign a vector3 value, however this vector3 is relative to the center of the object parents rather than the center of the world.

transform.localPosition = new Vector3(120f, 50f, 45f);

Using the car hierarchy example from above as an example, if we were setting the localPosition of the ‘Parts’ object here and the parent ‘Car’ object was positioned at X:50, Y0, Z:0 in world space then our ‘Parts’ object would end up being positioned at X:170, Y:50, Z:45 in world space.

How Unity depth works (parenting and child gameobjects)

In your Unity hierarchy gameobjects can be put inside each other (either by directly dragging them on to each other or via scripting). An object inside another object it is called the child and the containing object is called the parent. (and the process of moving an object inside another object is called parenting)

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

Explanation of a colour coded screenshot of a Unity hierarchy for a car

Moving an object with the Transform Translate method

The translate method allows you to move an object based on direction and distance. You can also set whether you want to move in local or world space with the second Space parameter.

The main parameter of the translate method is the translation which is a Vector3 value defining how much your object should move from its current position.

Once you have the direction you want to move in you then need to multiply it by your distance.

myTransform.Translate(myTransform.forward * 10f);

This will move the target transform forward by 10 meters.

Rigidbody Component

Rigidbody components are used when you want to give your objects physics. You add them manually in the inspector by adding the rigidbody component to a gameobject and reference them in scripts by either assigning a public Rigidbody variable or by calling GetComponent () from the gameobject or transform which the rigidbody component is attached to.

Directly set the rigidbody position

Similarly to setting the transform position, rigidbodies have position and localPosition properties which will directly move the object into world or local positions. HOWEVER this isn’t just a shortcut to setting the transform position! It uses the physics engine to set the position of the rigidbody and will update the transform position in the next physics update. You can read more about physics and timesteps here: https://www.unity3dtips.com/unity-fix-movement-stutter/

Using the rigidbody MovePosition method

The rigidbody MovePosition method uses the interpolation mode set on the rigidbody to move the rigidbody into the requested position over time, generally you would call it per FixedUpdate() and the value would change per frame with a value similar to how the transform translation method works.

myRigidbody.MovePosition(myTransform.position + myTransform.forward * 10f);

This will move the target rigidbody forward by 10 meters. Realistically the 10 value here would be multiplied by deltatime to gain a smooth movement, for more information check out this guide which explains this in more detail: https://www.unity3dtips.com/unity-fix-movement-stutter/

Using the rigidbody AddForce method

Adding force to a rigidbody works like you might expect by adding a thrust in a certain direction. Note that if your rigidbody has gravity enabled then your force will be fighting against gravity.

myRigidbody.AddForce(myTransform.forward * 10f, ForceMode.Force);

This will add an acceleration force to the rigidbody which is affected by the mass of the rigidbody. The force is being applied in the forward direction with a strength of 10.

Force modes

Unity Fundamentals — Moving a game object

Understand transform position and learn simple methods on how to move game objects in Unity.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

Resources

Basics first

Each object in Unity holds Transform. Transform is responsible for position, rotation, and scale manipulation of the object.

Each object placed in the Unity scene has some initial position, rotation, and transform.

The object is residing either in 3D or 2D space, depending on the type of the project. In our case, we will be working on a 3D project.

Let’s take a look at the example of a Capsule object located in 3D space.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The Center of the plane is located at the position (0,0,0) whereas the point in 3d space is determined by 3 values laying on the (X, Y, Z) axis.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

Each position on the axis holds a different value. Starting from the center position (0,0,0) going to the right is increasing X value, going upwards Y value is increasing and lastly going forward Z value is increasing. Going in the opposite direction of the mentioned axis causing a decrease of the value.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

In order to find a point in 3D space you need to know all 3 coordinates, (X, Y, Z)

Now let’s see where Capsule would be located at the position (1,1,1)

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The Capsule is now located exactly 1 Unity meter on X-axis, 1 meter on the Z-axis, and 1 meter up on Y-axis. Maybe, you are asking the question “Why is the capsule 1 meter on Y?”

Well, the Capsule object has 2 meters, so the center of the capsule is 1 meter above the ground.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

That’s why it needs to be placed 1 meter on the Y-axis. Otherwise, it would be half-way body in the middle of the plane.

Change position in the code

You can change the object position in the code by manipulating the transform property of the object.

Let’s move the game object to the position (1,1,1)

That’s are the basics you need to know about the position. Remember position is a location in the space represented by the (X, Y, Z) value, each laying on its own axis.

Now we are going to create a functionality to move the Capsule when a player is pressing input keys on the keyboard.

Movement script

First, create a new script called Movement.cs and add it to the Capsule object.

We will place movement functionality into the Update method. Update is executed during the entire play-time of the game — how often depends on the frame-rate of your game.

About Update

The Update method is called once per frame.

If the game is running at 140fps then Update is called 140 times per second. This is making it a great spot for functionality that should happen constantly during the game duration.

Handling player input is usually a functionality that is placed in the Update method.

Capture a player input

In order to move a game object, we need to capture a player input. If a player is using as the input device a keyboard, we want to capture as he is pressing arrow keys or AWSD keys on the keyboard.

For this purpose, we can write the following code in Update.

Horizontal Axis

Horizontal value of 0 means that NO “movement” key is pressed.

Value 1 means that the player is pressing the “ right” arrow or “ D” key (player wants to move right)

Vertical Axis

Vertical value of 0 means that NO “movement” key is pressed.

Value 1 means that the player is pressing the “ up” arrow or “ W” key (player wants to move forward)

So while holding “ right” arrow for one second you can get values in horizontal input, 0, 0.1, 0.3, 0.5, 0.7, 1, 1, 1, 1….reaching 1 value almost imidiately.

These are just illustrational values, you can get completely different ones.

Since we can capture the player input we are almost ready to move.

Ready to move

Let’s extend the current implementation.

You will notice one thing. We are moving too fast.

First, let’s explain the code above.

We are storing the vertical and horizontal values in the Vector3 structure, in order to apply them to change the game object position.

As I mentioned before, every game object has a position represented by (X, Y, Z) value.

We need to apply this change of position in form of Vector3 and then provide it to the Translate method. The Translate method will move a game object in direction and distance of translation.

Imagine these 3 cases:

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The object will move right when a player is holding the “ right” arrow or “ D” because Vector3 is holding value (1,0,0), which expresses a change in direction on X-axis.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The object will move forward when a player is holding the “ up” arrow or “ W” because Vector3 is holding a value (0,0,1), which expresses a change in direction on Z-axis.

How to move object in unity. Смотреть фото How to move object in unity. Смотреть картинку How to move object in unity. Картинка про How to move object in unity. Фото How to move object in unity

The object will move diagonally when a player is holding the “ up” arrow or “ W” at the same time as the “ right” arrow or “ D” key, because Vector3 is holding value (1,0,1), which expresses a change in direction on X and Z axis.

These changes are applied to the game object’s current position. You can think of the game object as the center of the plane.

Why is the object moving fast?

The game object is moving fast because Update is called many times per one second. Imagine stable frame-rate(FPS) to be 50. 50 FPS means that your game is updated 50 times per second therefore Update is called 50 times per second.

This also means that holding input keys for the length of 1 second would result in 50 executions of

so if Vector3 would be (1,0,0) then I would move each second 50 meters to the right because Update is called 50 times per second.

Our functionality is frame-dependent. In order to move only 1 meter per second, we need to decrease the value of the movement.

Time delta

First, update translate like this

If you will play the game now you will notice that movement is fixed and you are moving nice and slowly.

Time delta is the completion time of the last frame in seconds.

Again, imagine stable frame-rate 50 frames per 1 second. Completion time of each frame would be 0.02 second because 50 * 0.02 = 1 second

If you multiple the movement by 0.02 you will lower its value, if this happens 50 times per second then Vector3(0,0,1) will apply movement of 1 meter per second.

Now the functionality of the movement is time-dependent. You will use Time.deltaTime a lot.

Final improvements

If the movement is too slow then multiple the movement with some higher value. Let’s say you want to move the Capsule to move 6 meters per second.

Now the final code.

You can notice I called Normalize method on movement. In the case of diagonal movement, you will move faster. Normalize will “fix” it and movement will be the same in every direction.

I am explaining Normalize in much more detail in my other Youtube video: https://www.youtube.com/watch?v=oCU8Ew1XTbs&t=3s

Recap

Now you should have a basic understanding of how to move a game object in Unity. Everything is just about the change of the position over some time-interval.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *