Player -Color, Movement @ Unity Part 1

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…

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Is Chatbot your friend?

What to code in 2021?

The TWO objectives of Automation

Is interface implementation that difficult to understand?

[New Coin Listing] Sphynx Labs(SPHYNX) to Be Listed on DigiFinex

Google OAuth for Ruby On Rails

NEW EVENT “BUG BOUNTY”

How to install and configuration automation on Mac OS

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… !!

More from Medium

No Clutter Object Spawning in Unity

Create Enemy Container

The Power of Unity’s GetComponent

Unity Dev Blog: Damage VFX Using Animated Sprites in Unity and Triggering then in C# Code

Physics 101 — Simulating Physics in Unity