Unity3D: Simple Player Movement — 3
How to add boundaries for a player’s movement in a 2D Space Shooter.
This third tutorial will include steps to add boundaries to the player’s motion. Our objective will be to limit the player cube to the bottom half of the scene’s screen and to allow have it appear on the opposite side of the once it passes the edge.
First, we will script limits for the Y-motion. Just like in the void Start(), we will use the transform.position function within the void Update() to tell the player to remain at a certain point once it reaches a boundary.
The code will be written as such:
The player will transform its position….
….to a new Vector3 position, a.k.a. another XYZ position. 0 will be placed in inside the parentheses for X, Y and Z as a placeholder….
Again, we are trying to add boundaries for the y-motion, so the x-position is not a concern for this line of code. It will simply be the x component of the player’s new transformed position, otherwise written as transform.position.x.
Now for the y-position within the new Vector3 position, we will use a special function that will set limits for a variable’s values. It is written as Mathf.Clamp(). Inside its parentheses are listed the following values:
Clamp(float value, float min, float max)
*[Reference: docs.unity3d.com]
Within the Mathf.Clamp, we will be entering the values as such….
The float value to which the limits will be applied will be the transformed position for Y.
The float min will be the y-position when the cube reaches the bottom of the screen. According to the animation below, the cube reaches Y = -3.9 near the edge of the screen.
Since the value is a decimal, it is considered a float, so we will include an f after the value when we type it inside Mathf.Clamp().
The float max value will be near the starting position where Y equals 0.
To recap, the Mathf.Clamp() function is calling out the transform.position.y and setting its position limits between Y = -3.9 and Y = 0. Let us put it inside of the equation written earlier.
Lastly, our Z-position will be left at 0.
After we have saved and updated our scene in Unity, we will see that our player cube is limited in its Y-position.
Now, lets work on the X position boundary. Instead of using a Mathf.Clamp() function, we will use IF statements to write our script. Let us first see where our X positions are near the edges of the screen.
The cube disappears off the screen at X = -11.3 to the left and +11.3 to the right.
With our player’s movement in the X, we do not want the cube to simply stop at the edge. We want it to appear on the opposite side as if it teleported to the other side while undergoing constant movement.
Using the IF statement, our logic can be written out as follows:
If the Player passes the left edge of the screen (X = -11.3)….
….we want the Player to reappear on the opposite side of the screen (X = -11.3).
Notice that inside Vector3 we included the current transformed position for Y in the Y slot of the parentheses and 0 for Z.
Within the same IF statement, we can include an ELSE IF that simply applies the reverse of the previous statement. Instead of passing X = -11.3, it will be X = 11.3 and it will be teleported to X = -11.3.
After the script is saved and our Unity project is updated, the cube will now teleport from one side to another.
Success! This concludes the tutorial series on simple player movement. Settings can be adjusted, such as speed and the boundary limits. Below is a list of terms and functions that were included in this tutorial.
transform.position
transform.translate
Mathf.Clamp()
Vector3()
if and else if