Player -Color, Movement @ Unity Part 1

Anurag Chawla
3 min readMar 30, 2021

Player, Enemies, Car etc — Nothing exists when Unity Editor opens for first time, these are what called Objects more precisely Game Objects.

To define these Game Objects in Unity, one has to create these objects from scratch — whether it’s 2D or 3D.Creating Prototype of these objects and put life into it — like color , movement, boundaries where they exists, move & perform some kind of action / motion .These are called characteristics and behavior of these objects — created using Scripting APIs using C# or UnityEngine APIs.

Here take an example of Unity 3D Object Capsule, which is Game Object.

Note : This Game Object along with others like Main Camera, Direction Light have certain things in common — all having TransformIt’s used to store and manipulate the position, rotation & scale along X,Y & Z direction along with this having unique properties.

In order to Prototype Player, let’s create Capsule 3D Game Object & applying some color to it — creating Materials folder under Projects and adding new Material & applying Albedo (RGB and Transparency) that defined under Mesh Renderer of Game Object as shown below.

Adding Color to 3D Capsule.

For Movement- for the Player, one has to create a new C# Script and attached it to the 3D Object here Player as show below

Creating C# Scripts and Attaching to Objects

When you double click on it — opens in Visual Studio, that having class Player inherit from MonoBehavior that UnityEngine Provides having the Start and Update methods.

Start / Update Methods — from MonoBehaviour UnityEngine

In Unity Transform have properties position , consider an example where Player moves from the current position to the new position using the c# script below.

Script for taking the current position to new position
Here Capsule moves from current pos Y= -2.38 to new Y= 0 position

Similarly Transform having Translate method — give life for the movement of the Object i.e., right, left, up and down using Vector3 translation parameter passed to this method. i.e, public void Translate(Vector3 translation)

Shown above different Movements — Left, Right, Up and Down..Eg: move right 5 meters per sec using Vector3

Consider few examples :

a. Moves to right.. Its been called once per frame
transform.Translate(Vector3.right);

b. Its same as moving to +1 X axis right (1 meter/frame in real world) transform.Translate(new Vector3(1,0,0));

c. Its same as moving to -1 X axis left (1 meter/frame in real world) transform.Translate(new Vector3(-1,0,0));

d.To move 1 meter per sec incorporate using Time.deltaTime i.,e Time.deltaTime represents the time that passed since the last frame. transform.Translate(Vector3.right * Time.deltaTime);

e. To move 5 meter per second , multiple by 5 (eg moving speed =5 )i.e., Multiplying the moveSpeed variable by Time.deltaTime ensures that the GameObject moves at a constant speed every frame. transform.Translate(Vector3.right * 5 * Time.deltaTime);

Here above Move Player to the Right using Vector 3.Right * 5 * Time.deltaTime

More Importantly, adding these behavior , characteristics in more controlled manner are defined using Variables — see you in Part 2 .

@GameDevHQ-Day 5…

--

--

Anurag Chawla

Emerging Technologies - Power Virtual Agents | AI Chatbots | Multi-Cloud & DevOps | UI Technologies - React , Angular | Gaming Technolgies - Unity, XR- AR/VR