Module 2 & 3
Module 2 & 3
Collision theory
Collision detection is an essential element in game development. In Unity,
whenever two objects interact, a collision occurs. The UnityEngine offers
various ways of responding to collision events, whether by the use of Physics
or by the custom C# scripts.
Types of colliders
Unity offers different types of colliders each of which are given below.
Here are some of the key features of the collision system in Unity.
Collision response: The physics engine determines how the objects should
respond when they collide. It usually involves a change in the speed and
direction.
Bouncing back of a ball whenever it hits the wall is an example of collision
response.
To detect collision between two colliders, you can make use of the collision
detection functions mainly OnCollisionEnter(). It gets called as soon as the
two GameObjects collide with each other.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Explanation
https://www.educative.io/answers/introduction-to-collision-
detection-in-unity
The scripts in Unity can detect whether collision occurs and returns the
response actions using the OnCollisionEnter function. However, the physics
engine can be used to detect whenever one collider enters the space of
another collider without creating a collision.
A collider (on which the Is Trigger property is set) does not behave as a
solid GameObject. It allows other colliders to pass through it. Whenever a
collider enters the space of other GameObjects,
the OnTriggerEnter() function gets called on the object.
Trigger response and events: Unity engine also offers trigger colliders
which are used whenever a GameObject enters or exits the specified area.
The main functions associated with the trigger events
are OnTriggerEnter(), OnTriggerStay(), and OnTriggerExit(). These functions
can be used whenever the corresponding events happen.
Note:
Example
You can define a response, such as increasing the player’s score, playing a
sound, or displaying a message, whenever a trigger detects that the player
has entered the specified area.
These responses can be implemented using C# scripts attached to
the GameObjects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Gets called during the stay of object inside the collider area
Explanation
For Demonstration
https://www.educative.io/answers/introduction-to-collision-
detection-in-unity
If you want the object to react to physics (e.g., falling or moving with
gravity), you should add a Rigidbody component to the object.
To add a Rigidbody:
3. Collision Detection
4. Scripting (Optional)
Here’s an example script that detects when an object collides with the floor:
csharp
Copy
using UnityEngine;
if (collision.gameObject.CompareTag("Floor"))
}
}
In this script:
5. Testing
Ensure that the object (e.g., a player or falling object) is moving and
reacts with the floor when they collide.
Creating characters and floors in Unity involves several steps, from designing
the assets to setting up the necessary components to make them interact
properly. Here’s a step-by-step guide to creating characters and floors in
Unity.
1. Create a Plane:
o You can resize the plane by adjusting its Scale in the Inspector
window to make it bigger if needed (e.g., set X and Z to 10, 10
for a larger area).
1. Create a Terrain:
o This will create a large, editable terrain where you can shape and
texture the floor.
o In the Inspector, use the Terrain tools to sculpt the terrain. You
can raise, lower, and smooth the ground as you like.
3. Add Textures:
o You can paint textures onto the terrain using the Paint Texture
tool in the Terrain component. Add materials like grass, dirt, or
rocks to create a more detailed floor.
2. Creating a Character
1. Create a Cube:
o This will allow the cube to interact with gravity and other
physics-based elements like the floor.
3. Add a Collider:
csharp
Copy
using UnityEngine;
transform.Translate(movement);
o Attach this script to your Cube (or any other object you want to
act as your character).
o You can now move the cube around using the arrow keys or
WASD.
3. Add a Collider:
csharp
Copy
using UnityEngine;
controller = GetComponent<CharacterController>();
velocity.y = -2f;
controller.Move(velocity * Time.deltaTime);
MODULE 3
In Unity, you can apply a color filter to a camera by using
post-processing effects or by modifying the camera's
image through scripts. Here's how you can do both:
1. Using Post-Processing (Recommended)
Post-processing is a powerful tool in Unity that allows you
to apply various effects, including color filters, to your
camera.
Steps to apply a color filter using Post-Processing:
1. Install Post-Processing Package:
o Open Unity's Package Manager (Window >
Package Manager).
o Search for "Post Processing" and install it.
2. Add Post-Processing Volume:
o In the hierarchy, right-click and choose Create
> Volume > Global Volume (or local volume if
needed).
o In the Inspector window, click Add Component,
and add the Post Process Volume component.
o Make sure Is Global is checked if you want the
effect applied globally.
3. Create or Use a Post-Processing Profile:
o Click the New button next to the Profile field in
the Post Process Volume component.
o A new Post-Processing profile will be created. You
can modify this profile later.
4. Add the Color Grading Effect:
o In the Post-Processing Profile, click Add Effect >
Color Grading.
o In the Color Grading settings, you can adjust
the Post-Processing settings:
Tone Mapping: Select a tone mapping
algorithm to affect the overall colors.
Saturation: Increase or decrease saturation
to change how vibrant the colors are.
Temperature & Tint: Adjust the color
temperature and tint to add a color filter
(e.g., blue, orange).
Lift, Gamma, Gain: These control
shadows, midtones, and highlights. You can
use these to add more specific color
adjustments.
5. Play the Scene:
o When you run the scene, the camera should
apply the color filter as per the settings you
configured in the Post-Processing Profile.
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : POSITION;
};
float4 _Color;
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
[RequireComponent(typeof(Camera))]
public class CameraColorFilter : MonoBehaviour
{
public Material colorFilterMaterial;
private void OnRenderImage(RenderTexture src,
RenderTexture dest)
{
if (colorFilterMaterial != null)
{
Graphics.Blit(src, dest, colorFilterMaterial);
}
else
{
Graphics.Blit(src, dest);
}
}
}
4. Apply the Script to the Camera:
o Create a new Empty GameObject in the scene
and add the CameraColorFilter script to it.
o Assign the material you created earlier to the
colorFilterMaterial field in the script.
void Update()
{
// Get scroll input
float scrollInput = Input.GetAxis("Mouse
ScrollWheel");
void Update()
{
// Get scroll input
float scrollInput = Input.GetAxis("Mouse
ScrollWheel");
void Update()
{
// Zoom in with W key
if (Input.GetKey(KeyCode.W))
{
camera.fieldOfView -= zoomSpeed *
Time.deltaTime;
}
Switching Cameras
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.
We then need to create two additional camera on the
Scene and place them where we want it to. Only set the
camera you want the players to see first active and the
other camera inactive.
Last we create an empty GameObject called CamManager
to store the script, and link the cameras to the script.