In this guide I just want to quickly go over a simple way to switch between cameras in Unity. We can achieve this by setting different cameras on or off using SetActive().
We first start by deciding on the condition we use to change the camera. For now we’ll use Input buttons 1, 2 and 3 to switch between cameras. We go into Project Settings -> Input Manager and add three new items to the list, which we’ll label Switch1, Switch2, and Switch3. Each correspond to the button on the keyboard.
Then we write a C# script called CamSwitch. All we need is three public GameObjects which we’ll label cam1, cam2, and cam3 for each of the cameras we’ll be switching into. Inside the Update function, depending on which button we press, we’ll turn the corresponding camera active while setting the other cameras false. …
There are two simple methods of writing scripts for the mouse to interact with the game object. A simple method is to write a script for each game object where on mouse down or up, execute some code. However, you can also have a raycast to spawn at where the mouse is and if it hits an game object that you want, execute some code.
This is one of the easiest script to write for a game object. All you need to do write the interaction code in the function OnMouseDown() and attach it to the game object.
Unity ray cast can also do the trick, first make three variable, the Ray, the objects it hits, and lastly to store the Target of your choice. Inside the Update function, if we left click, we get a list of all the target hits, for then we check for tags. For each game object that has the tag target, we know we got the object and then we can do anything we want with it. …
Unity have a lot of special event functions that gets called automatically by the engine. For example Start and Update function is added to every script by default.
In this post I want to talk about the difference between two similar functions that can easily be mixed up with each other, Start and Awake.
Start and Awake function work also identical, both are called near the beginning with Awake being called earlier. However there is one key difference between the two, on Start will not be called in the script component is disabled.
Usually Unity coders will use Awake to create component references and initializing variables. While Start will be used to access those data to avoid errors. …
In unity there are two basic method of moving an object. Using translate on the object itself, or using a rigidbody.
Moving with translate is easy. All you need is to grab the transform (object) and apply a translate which includes a vector 2 for direction, then speed and time.
However, the issue with translate is that it’ll ignore every physic and collision detection for the object. Which means the character will ignore any other object in the scene. However this is really good for adding or showing any effects that doesn’t interact with the world.
There are three method of moving with RigidBody, use the function AddForce(), set the velocity, or the function MovePosition(). The setup is simple, add a RigidBody, create a variable for speed, and make a Vector2 for movement. …
This is just a simple guide for saving and loading data from PlayerPrefs in Unity.
PlayerPrefs system in Unity allows users to store and access player data between game sessions. It stores data in a plain text file which makes any data easy to access but also easy to hack. Therefore try not to store any important data in PlayerPrefs.
Before starting, note that PlayerPrefs allows users to store 3 different types of data:
First I create an empty game object in Unity and call it GameController, while giving it the tag GameController as well. Then I create a new C# script where I’m going to edit and read data from, called PrefsControl.
You can easily access these functions from any other script, just find the game object first, then find the script and access it from there.
There are many ways to pass data from one scene to another inside Unity.
Today I want to showcase the easiest method of them all, using Static Keyword.
There are three types of variables in unity.
Public Variables are variables that can be accessed from other classes. Which allows other game scripts to grab variables from this script, and they are allowed to manipulate it however they want.
Private Variables are variables that can only be accessed from within the class itself. This is the most secure type of variables since other scripts cannot change it, unless the script itself creates public functions that access the variable itself. …
This is my list that I believe is important when creating UI elements in Unity.
Everything in the Unity UI can be organized inside an empty gameObject. UI elements that is similar should be grouped together under the same gameObject. Not only is this better for organization, it’s also useful when you want to turn certain elements on or off and making changes to them. Where instead of moving each UI element one by one, you can move the group and modify the group all at once.
When moving and changing objects around, the position and width/height tend to end up with weird decimal numbers. …
Recently I created an Unity 2D project, and recently came into issues with my project once I loaded the project to my laptop. My laptop isn’t anywhere as good as my desktop, therefore very often I would run into frame issues where instead of normal 60 frames per second, I would have around 20–30 frames per second. This has caused my project multiple issues such as objects moving slower than others, while certain other effects will appear normally. …
As a programmer we have seen the Big O notation many times, yet I noticed this is one concept that a lot of beginner programmers have a hard time understanding. It’s not the concept is hard to understand, it basically describe the performance or the complexity of an algorithm. Most people tend to overthink it and in their head they believe it’s an extremely complicated mathematic thing that you need to major in math to understand it.
The big O notation is basically a way to tell how long it takes for a certain code to run in respect to the amount of elements/items. There are many different notations but the few basic notations are O(1), O(n), O(n²), O(2^n) and O(log N). We usually use this to determine how effective an algorithm is if we are dealing with an large amount of data. …
There is a feature that was being experimented on in ruby which is the “Right-ward Assignment Operator”.
By current day standard, we assign values to variable using a “left”-ward assignment operator. Where the variable is on the left, and the value is on the right.
variableName = variableValue
This new feature reverse that operator and allows us to switch the direction where the value on the left, and and variable name is on the right.
variableValue => variableName
For a lot of programmers this might seem backwards and make it harder to read code. Especially for JavaScript programmers who use arrow functions daily. However I can see this becoming very useful for beginner programmers or non programmers since this method of assigning variables is the structure on how Math is used. …
About