Input.Getaxis, OnMouseDown, GetComponent< > - Unity Scripts

In This Blog We Will Discuss Some more Keywords.

Input.GetAxis :


In this picture a chunk of code is placed. You will see that Input.GetAxis("Vertical") returns a value.
The value is from 1 to -1. When there is no key pressed the value is zero.
Actually it returns the value of virtual axis. If we use Debug.log then the console will show some value on clicking the right or left button that will be constantly changing.



 Other thing is that Vertical is pointing towards the built in inputs. As you can see in the picture above that the vertical have some columns in it. Where the vertical have positive and negative button which give positive or negative float value on event pressed. Whereas if we don't want to use these inputs . We can write code instead of writing Vertical or horizontal in the brackets of input.getaxis. We can write it as Input.GetAxis(keycode.space); .  Here the space key is assigined and it will return some value.

OnMouseDown() :

OnMouseDown() method is actually used for the action performed against the mouse button click.
Whatever we write in the method that will be performed quickly.


As the code will apply  force on some object. That will move forward on the code instruction. Where as the gravity is true that the object will fall on the floor. 

GetComponent< > :

Lets see some scripts through which i can made clear understanding of the GetComponent< >.
The getcomponent requires type in it. Here are three scripts .

 Here are the two scripts in which a variable is declared. 

Here is the script in which GetComponent is used. Actually the this keyword is used to access the other scripts the data can be used in this script . Where the Syntax of doing this is given below with an example so that you can understand. 

Here the two classes are declared the in the awake method the s1 is taking the script data in s1. Similar with the s2.
Now in the start on the console the values in the following variables are displayed.




Comments

Post a Comment

Popular posts from this blog

How to move an object in Unity 3D

Vector Math - Unity