Unity Direction



Don't use transform.forward and transform.right to make your move vector, just make it in world space. Then, you can set transform.forward to the move direction. Also, as derHugo mentioned below in a comment, you should. Avoid using exact equality to compare floats. Instead use Mathf.Approximately or use your own threshold like below. Avoid setting triggers every frame. Instead you can use a. The principle of unity of direction is one Henri Fayol's 14 administrative principles. In this lesson, we will discuss what the principle is, the concepts behind it, and why it is important. Submission failed. For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

  • Unity Tutorial
  • Unity Useful Resources
  • Selected Reading

In this lesson, we will write code that makes a gameObject move up, down, left and right based on the user’s input. This should help us understand the workflow of Unity scripting more easily.

Remember that every GameObject has at least one component − Transform. What is special is that the Transform of a gameObject also shows up as variables in the scripting side of Unity so we can modify it via code. This is not restricted to the Transform either; all components in Unity have properties, which are accessible through variables in scripting.

Let us start with our movement script. Create a new script, and name it “Movement”.

Unity

Now, open the script and you should see the same stuff you saw in the last lesson.

Let us create a public float variable named speed. Making a variable public in Unity has a great advantage −

  • The variable shows up as a modifiable field inside the editor, so you don’t have to manually adjust the values in code.

If we save this script without touching the other methods, it should compile in Unity.

(You can see when it is compiling by the icon in the bottom right corner.)

Next, drag and drop the script from the Assets onto the GameObject. If you do it correctly, this is what you should see in the GameObject’s properties −

Since the speed value is adjustable and need not be changed in code all the time, we can use update() method instead of start().

Let us now consider the objectives for the Update method −

  • Check for the user input.

  • If there is a user input, read the directions of input.

  • Change the position values of the object’s transform based on its speed and direction. To do so, we will add the following code −

Let us now discuss the code in breif.

First of all, we make a floating point variable named h (for horizontal), and its value is given by the Input.GetAxisRaw method. This method returns -1, 0 or 1 depending on which key the player has pressed on the up/down/left/right arrows.

The Input class is responsible for getting input from the user in the form of key presses, mouse input, controller input, and so on. The GetAxisRaw method is slightly harder to understand, so we’ll get back to that later.

Next, we are updating the position of our gameObject to a new position defined by creating a new Vector2. The Vector2 takes 2 parameters, which are its x and y values respectively. For the x value, we provide the sum of the object’s current position and its speed, effectively adding some amount every frame the key is pressed to its position.

Save this script and head back to Unity. Unity will automatically update all scripts once it compiles successfully, so you don’t have to reattach the script again and again.

Direction

Now that you are done, change the value of the speed in the GameObject’s properties to say 0.8. This is important because a higher value will make the player move too fast.

Now, click Play and see your first small game in action!

Try pressing the arrow keys and moving around. To stop the game, simply press Play again. You can even adjust the speed in real-time so you do not have to stop and start it all the time.

In the next lesson, we will learn about rigidbodies and collisions.

Direction
  • Unity Tutorial
  • Unity Useful Resources
  • Selected Reading

In this lesson, we will write code that makes a gameObject move up, down, left and right based on the user’s input. This should help us understand the workflow of Unity scripting more easily.

Remember that every GameObject has at least one component − Transform. What is special is that the Transform of a gameObject also shows up as variables in the scripting side of Unity so we can modify it via code. This is not restricted to the Transform either; all components in Unity have properties, which are accessible through variables in scripting.

Let us start with our movement script. Create a new script, and name it “Movement”.

Now, open the script and you should see the same stuff you saw in the last lesson.

Let us create a public float variable named speed. Making a variable public in Unity has a great advantage −

  • The variable shows up as a modifiable field inside the editor, so you don’t have to manually adjust the values in code.

Unity Direction

If we save this script without touching the other methods, it should compile in Unity.

(You can see when it is compiling by the icon in the bottom right corner.)

Next, drag and drop the script from the Assets onto the GameObject. If you do it correctly, this is what you should see in the GameObject’s properties −

Since the speed value is adjustable and need not be changed in code all the time, we can use update() method instead of start().

Let us now consider the objectives for the Update method −

  • Check for the user input.

  • If there is a user input, read the directions of input.

  • Change the position values of the object’s transform based on its speed and direction. To do so, we will add the following code −

Let us now discuss the code in breif.

Direction

First of all, we make a floating point variable named h (for horizontal), and its value is given by the Input.GetAxisRaw method. This method returns -1, 0 or 1 depending on which key the player has pressed on the up/down/left/right arrows.

The Input class is responsible for getting input from the user in the form of key presses, mouse input, controller input, and so on. The GetAxisRaw method is slightly harder to understand, so we’ll get back to that later.

Next, we are updating the position of our gameObject to a new position defined by creating a new Vector2. The Vector2 takes 2 parameters, which are its x and y values respectively. For the x value, we provide the sum of the object’s current position and its speed, effectively adding some amount every frame the key is pressed to its position.

Save this script and head back to Unity. Unity will automatically update all scripts once it compiles successfully, so you don’t have to reattach the script again and again.

Now that you are done, change the value of the speed in the GameObject’s properties to say 0.8. This is important because a higher value will make the player move too fast.

Now, click Play and see your first small game in action!

Unity Direction

Try pressing the arrow keys and moving around. To stop the game, simply press Play again. You can even adjust the speed in real-time so you do not have to stop and start it all the time.

Unity Directional Light

In the next lesson, we will learn about rigidbodies and collisions.