8/7/2023 0 Comments Unity learn beginning script![]() The Vector2 takes 2 parameters, which are its x and y values respectively. Next, we are updating the position of our gameObject to a new position defined by creating a new Vector2. The GetAxisRaw method is slightly harder to understand, so we’ll get back to that later. The Input class is responsible for getting input from the user in the form of key presses, mouse input, controller input, and so on. This method returns -1, 0 or 1 depending on which key the player has pressed on the up/down/left/right arrows. = new Vector2 ( + (h * speed),įirst of all, we make a floating point variable named h (for horizontal), and its value is given by the Input.GetAxisRaw method. To do so, we will add the following code −įloat h = Input.GetAxisRaw(“Horizontal”) If there is a user input, read the directions of input.Ĭhange the position values of the object’s transform based on its speed and direction. Let us now consider the objectives for the Update method − Since the speed value is adjustable and need not be changed in code all the time, we can use update() method instead of start(). If you do it correctly, this is what you should see in the GameObject’s properties − Next, drag and drop the script from the Assets onto the GameObject. ![]() (You can see when it is compiling by the icon in the bottom right corner.) If we save this script without touching the other methods, it should compile in Unity. The variable shows up as a modifiable field inside the editor, so you don’t have to manually adjust the values in code. ![]() Making a variable public in Unity has a great advantage − Let us create a public float variable named speed. Now, open the script and you should see the same stuff you saw in the last lesson. Create a new script, and name it “Movement”. This is not restricted to the Transform either all components in Unity have properties, which are accessible through variables in scripting. 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. Remember that every GameObject has at least one component − Transform. This should help us understand the workflow of Unity scripting more easily. ![]() In this lesson, we will write code that makes a gameObject move up, down, left and right based on the user’s input. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |