Game Development Reference
In-Depth Information
Objective complete - mini debriefing
We just created a script that controls the movement of our character and his animation.
First, we created the parameters to use in our script. We also used the const keyword in
C# and the final keyword in Unity JavaScript to create the constant variables.
Note
The const and final keywords
We used the const keyword in C# to specify that the value of the field or local variable is
constant, which means it can't be modified from anywhere else.
However, there is no const keyword in Unity JavaScript, so we use the final keyword
instead; there is a slight difference between the final and const keywords. The const
keyword can only applied to a field whose value is to be known at compile time, such as
const float . So, this means that we can't use the const keyword with Vector3 ,
Rect , Point , and all the struct that are included in Unity. On the other hand, we will use
readonly instead. However, the final keyword can be used in the both cases.
For more details, visit http://tutorials.csharp-online.net/
CSharp_FAQ%3A_What_are_the_differences_between_CSharp_and_Java_constant_declarations .
Then, we get _animator , _boxCollider2D , and _height in the Awake() function
to check the animation state while it is moving. Then, we set all the variables to their de-
fault value. Next, we created the Flip() function, which will set _ isFacingRight to
true or false depending on the character movement. This function also sets the local x
scale to 1 if the character is facing right and -1 if it's facing left.
The next function that we created is the Grounded() function that uses Physic-
s2D.Raycast to check whether our character is on the ground or not.
In the Update() function, we get the character's horizontal movement by using In-
put.GetAxis("Horizontal") . Then, we check for the movement direction and call
the Flip() function. After that, we call the Grounded() function to check whether the
character is on the ground or not. If not, we can make the character jump by using In-
put.GetButtonDown("Jump") . Next, we update our camera position by using Cam-
Search WWH ::




Custom Search