EXPERIMENT - 03
Aim: Analyze the 2D rigid body transformation in Unity hub
1.1 CO Attained: CO1,CO2
1.2 Objective:
In this experiment, the 2D rigid body transformation is analyzed. A 2D rigid body
object transformation in Unity involves manipulating its position, rotation, and scale
while taking into account the physics simulation.
1.3 Resources: Unity Software
1.4 Description: Install the Unity's C# scripting language and execute the following codes
to analyize the rigid body transformation
Step 1: Translation: Translating or moving the object in 2D space.
using UnityEngine;
public class TranslationExample : MonoBehaviour
{
public float moveSpeed = 5f;
void Update()
{
// Move horizontally and vertically based on user input
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Translate the object
Vector2 translation = new Vector2(horizontalInput, verticalInput) *
moveSpeed * Time.deltaTime;
transform.Translate(translation);
}
}
Step 2: Rotation : Rotating the object in 2D space.
using UnityEngine;
public class RotationExample : MonoBehaviour
{
public float rotationSpeed = 180f;
void Update()
{
// Rotate based on user input
float rotationInput = Input.GetAxis("Rotation");
// Rotate the object
float rotationAmount = rotationInput * rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3.forward * rotationAmount);
}
}
Step 3 Scaling : Scaling the object in 2D space.
using UnityEngine;
public class ScalingExample : MonoBehaviour
{
public float scaleSpeed = 1.2f;
void Update()
{
// Scale based on user input
float scaleInput = Input.GetAxis("Scale");
// Scale the object
float scaleFactor = Mathf.Pow(scaleSpeed, scaleInput *
Time.deltaTime);
transform.localScale *= scaleFactor;
}
}
Step 4 Custom Transformations: Applying a combination of translation,
rotation, and scaling.
using UnityEngine;
public class CustomTransformationExample : MonoBehaviour
{
public float moveSpeed = 5f;
public float rotationSpeed = 180f;
void Update()
{
// Move horizontally based on user input
float horizontalInput = Input.GetAxis("Horizontal");
Vector2 translation = new Vector2(horizontalInput, 0f) * moveSpeed *
Time.deltaTime;
transform.Translate(translation);
// Rotate based on user input
float rotationInput = Input.GetAxis("Rotation");
float rotationAmount = rotationInput * rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3.forward * rotationAmount);
}
}
Viva Questions:
Q. 1 Explain 2D rigid body?
Q. 2 Explain Translation, Rotation and Scaling?
Q. 3 What are 3 I’s in Virtual Reality?