Player -Movement @ Unity Part 2

Controlling Players movements..!!

Unity — having Input Manager , opens from Edit, Project Properties -> This is where one can configure the controls to use with Unity Engine Input API.

It has 18 different Axes to support from like Horizontal , Vertical , Fire 1, Fire 2, Fire 3, Jump etc .

One of those properties are Horizontal (Left,Right) or (a, d of keystroke of keyboard) and Vertical (Up, Down) or (w, s of keystroke of keyboard) that maps to movement of any 3D Game Object.

Input Manager- For Horizontal and Vertical Axes

How these movements can be controlled — this can be done using Variables.

Variables -This is name given to piece of memory in computer to store different information or data, so that it can be accessed later during course of execution.

In Unity , its using C# Scripting APIs and follow the same rules of C# data types where they having scope— Public, Private or Local Scope

It follow convention as below

[scope][datatype][name of variable] =[optional assignment value]

  1. Public -accessed by any other class or program within the same project. public float _speed = 8.0f;

2. Private- accessed within the same class. In Unity , Private variables in-order accessed by Inspector to make changes at run-time or by designer using SerializableField attribute i.e., private float _speed = 8.0f;

[SerializedField]- This attributes makes _speed variable visible in Inspector

3. Local Scope — can be accessed within the method of the class as shown below

Horizontal & Vertical Input Variables

If you now see in above examples , we have defined the variables of local scope— i.e, horizontalInput and verticalInput that maps to the InputManager -> Horizontal, Vertical Axes and variable of private scope -i.e, _speed having [SerializeField] attribute, so that it can be changed from the inspector.

So player movement along the horizontal /vertical direction based on speed and moving Time.deltaTime as under :

or it can be more optimized in single statement as shown below

Movement of Player and Speed -> can be controlled by developer using Inspector here..

@GameDevHQ-Day 6…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anurag Chawla

Anurag Chawla

I am a passionate technologist with a strong aptitude for new skills that includes Exploring and Learning Unity 3D Scripting APIs… !!