0% found this document useful (0 votes)
16 views8 pages

ARVR Unity Programs-For Manual

The document provides step-by-step instructions for five basic Unity programs aimed at undergraduate students, covering essential concepts such as object movement, color change on click, jumping mechanics, first-person character control, and simple AI behavior. Each program includes clear objectives and detailed scripting guidance to facilitate learning. The exercises are designed to be beginner-friendly and progressively build on Unity's foundational concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

ARVR Unity Programs-For Manual

The document provides step-by-step instructions for five basic Unity programs aimed at undergraduate students, covering essential concepts such as object movement, color change on click, jumping mechanics, first-person character control, and simple AI behavior. Each program includes clear objectives and detailed scripting guidance to facilitate learning. The exercises are designed to be beginner-friendly and progressively build on Unity's foundational concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Below are 5 basic Unity programs with detailed step-by-step instructions for undergraduate

students. These programs cover essential Unity concepts such as scene setup, scripting, object
manipulation, user input, and interaction. Each exercise is designed to be beginner-friendly
and progressively builds on Unity's basic concepts.

Program 1: Moving an Object with Arrow Keys

Objective: Learn how to move an object using keyboard input.

Steps:

1. Create a New Unity Project:


o Open Unity Hub and click New Project.
o Choose the 3D template and name the project (e.g., "MoveObject").
o Click Create to open your new project.
2. Create the Object to Move:
o In the Hierarchy panel, right-click and select 3D Object → Cube.
o This will create a cube in the scene.
3. Add a Script to Move the Cube:
o In the Project panel, right-click in the Assets folder and select Create → C#
Script.
o Name the script MoveCube.
o Double-click on the script to open it in Visual Studio.
4. Edit the Script:
o Replace the existing code with the following:

using UnityEngine;

public class MoveCube : MonoBehaviour


{
public float speed = 5f;

// Update is called once per frame


void Update()
{
float moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float moveZ = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(moveX, 0f, moveZ);
}
}

5. Attach the Script to the Cube:


o Go back to the Unity Editor and drag the MoveCube script from the Assets
folder onto the Cube in the Hierarchy panel.
6. Test the Movement:
o Press Play at the top of the Unity Editor.
o Use the arrow keys or WASD keys to move the cube in the scene.
o The cube should move based on your input.
Program 2: Changing Object Color on Mouse Click

Objective: Change the color of an object when the user clicks on it.

Steps:

1. Create a New Scene:


o Open Unity, and create a new scene if not already done.
2. Add a 3D Object (Sphere):
o Right-click in the Hierarchy and select 3D Object → Sphere.
o Rename the sphere to ColorChangingSphere.
3. Create a Script to Detect Mouse Click:
o In the Project panel, right-click in Assets and select Create → C# Script.
o Name the script ChangeColorOnClick.
o Double-click the script to open Visual Studio.
4. Write the Script:
o Replace the code in ChangeColorOnClick with the following:

using UnityEngine;

public class ChangeColorOnClick : MonoBehaviour


{
private Renderer rend;

void Start()
{
rend = GetComponent<Renderer>();
}

void OnMouseDown()
{
rend.material.color = new Color(Random.value, Random.value,
Random.value); // Set to random color
}
}

5. Attach the Script to the Sphere:


o Go back to Unity, and drag the ChangeColorOnClick script onto the
ColorChangingSphere in the Hierarchy panel.
6. Test the Click Interaction:
o Press Play.
o Left-click on the sphere, and it should change color each time you click on it.

Program 3: Simple Jumping Mechanism

Objective: Implement a basic jumping mechanic using Unity’s physics system.


Steps:

1. Create a New Unity Project (if not already done):


o Choose the 3D template.
2. Add a Player Object (Cube):
o Right-click in the Hierarchy → 3D Object → Cube.
o Rename it to Player and resize it to be more like a player character (e.g., set
the scale to (1, 2, 1)).
3. Add a Plane to Act as Ground:
o Right-click in the Hierarchy → 3D Object → Plane.
o This will act as the ground for the player to jump on.
4. Add a Rigidbody Component:
o Select the Player object.
o In the Inspector, click Add Component → Rigidbody.
o This component enables the cube to interact with Unity’s physics system.
5. Create the Jumping Script:
o In the Project panel, right-click in Assets → Create → C# Script.
o Name the script PlayerJump.
o Double-click to open the script.
6. Write the Script:
o Replace the script with the following code:

using UnityEngine;

public class PlayerJump : MonoBehaviour


{
public float jumpForce = 5f;
private Rigidbody rb;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.velocity.y) <
0.01f)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}

7. Attach the Script to the Player:


o Go back to Unity, drag the PlayerJump script onto the Player object in the
Hierarchy panel.
8. Test the Jumping:
o Press Play.
o Press the Spacebar to make the cube jump.
UNITY Programs

Program 1: First-Person Character Controller with Mouse Look

Objective: Create a first-person character controller where the user can move around using
keyboard input and look around using the mouse.

Steps:

1. Create a New Unity Project:


o Open Unity Hub, click New Project, and select 3D.
o Name your project (e.g., FirstPersonController) and click Create.
2. Add Terrain and Player Object:
o Right-click in the Hierarchy → 3D Object → Terrain.
o Right-click in the Hierarchy → 3D Object → Cube.
o Rename the cube to Player (this will represent the player's body).
3. Create the Player Script:
o Right-click in Assets → Create → C# Script and name it PlayerController.
o Open the script and write the following code:

using UnityEngine;

public class PlayerController : MonoBehaviour


{
public float speed = 5f;
public float mouseSensitivity = 2f;
public Transform playerBody;

private float xRotation = 0f;

void Update()
{
// Handle mouse look
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);

playerBody.Rotate(Vector3.up * mouseX);
Camera.main.transform.localRotation = Quaternion.Euler(xRotation,
0f, 0f);

// Handle player movement


float moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float moveZ = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(moveX, 0f, moveZ);
}
}

4. Add the Script to the Player:


o Drag the PlayerController script onto the Player object in the Hierarchy.
5. Create and Attach the Camera:
o Right-click in the Hierarchy → Camera.
o Position the camera so that it’s inside the Player cube.
o Drag the Camera into the PlayerBody field in the PlayerController script
(in the Inspector).
6. Test the First-Person Controller:
o Press Play. You should be able to move the player using WASD and look
around using the mouse.

Program 2: Basic 3D Racing Game (Car Movement and Camera Follow)

Objective: Create a simple racing game with a controllable car and a camera that follows the
car's movement.

Steps:

1. Create a New Unity Project:


o Open Unity and create a new 3D project (name it 3DRacingGame).
2. Import a Car Model or Use Cube:
o For simplicity, you can use a Cube for the car.
o Right-click in Hierarchy → 3D Object → Cube and rename it to Car.
o Resize the cube to look more like a car (e.g., (2, 1, 5)).
3. Create the Car Movement Script:
o Right-click in Assets → Create → C# Script, name it CarController.
o Open the script and add the following code:

using UnityEngine;

public class CarController : MonoBehaviour


{
public float moveSpeed = 10f;
public float turnSpeed = 100f;

void Update()
{
float move = Input.GetAxis("Vertical") * moveSpeed *
Time.deltaTime;
float turn = Input.GetAxis("Horizontal") * turnSpeed *
Time.deltaTime;

transform.Translate(Vector3.forward * move);
transform.Rotate(Vector3.up * turn);
}
}

4. Attach the Script to the Car:


o Drag the CarController script onto the Car object in the Hierarchy.
5. Create a Camera Follow Script:
o Create another script and name it CameraFollow.
o Write the following code to make the camera follow the car:

using UnityEngine;

public class CameraFollow : MonoBehaviour


{
public Transform target; // Target to follow (Car)
public Vector3 offset; // Offset from the car

void Update()
{
transform.position = target.position + offset;
transform.LookAt(target);
}
}

6. Attach the Camera Follow Script:


o Drag the CameraFollow script onto the Main Camera.
o In the Inspector, assign the Car object to the target field and adjust the offset
for the desired camera angle (e.g., (0, 5, -10)).
7. Test the Racing Game:
o Press Play. You should be able to control the car using WASD or the arrow
keys, and the camera will follow the car.

Program 3: Simple 3D Platformer (Jump and Collectibles)

Objective: Create a basic 3D platformer where the player can jump and collect items.

Steps:

1. Create a New Unity Project:


o Create a new 3D project and name it 3DPlatformer.
2. Create the Player Object:
o Right-click in the Hierarchy → 3D Object → Cube, and rename it Player.
o Resize the cube to represent the player (e.g., (1, 2, 1)).
3. Add a Rigidbody Component:
o Select the Player and click Add Component → Rigidbody to allow the cube
to interact with Unity's physics system.
4. Create a Jumping Script:
o Create a C# Script named PlayerJump and write the following code:

using UnityEngine;
public class PlayerJump : MonoBehaviour
{
public float jumpHeight = 5f;
private Rigidbody rb;

void Start()
{
rb = GetComponent<Rigidbody>();
}

void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.velocity.y) <
0.01f) // Check if on the ground
{
rb.AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
}
}
}

5. Attach the Script to the Player:


o Drag the PlayerJump script onto the Player object.
6. Create a Collectible Item (Cube):
o Right-click in Hierarchy → 3D Object → Cube.
o Rename it to Collectible and place it somewhere the player can reach.
o Attach a Collider (Box Collider) to the Collectible and check Is Trigger.
7. Create a Collecting Script:
o Create a new C# Script called CollectibleItem and write the following code:

using UnityEngine;

public class CollectibleItem : MonoBehaviour


{
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
Destroy(gameObject); // Destroys the collectible when the
player touches it
}
}
}

8. Tag the Player:


o Select the Player and in the Inspector, set the Tag to Player.
9. Test the Platformer:
o Press Play and test jumping and collecting items.

Program 4: Simple AI Enemy with Patrol and Attack

Objective: Create a simple enemy AI that patrols between points and attacks the player when
in range.
Steps:

1. Create a New Unity Project:


o Create a new 3D project and name it EnemyAI.
2. Create the Player Object:
o Right-click in the Hierarchy → 3D Object → Cube, and rename it to Player.
3. Create the Enemy Object:
o Right-click in the Hierarchy → 3D Object → Cube, and rename it to
Enemy.
4. Create the Patrol and Attack Script:
o Create a C# Script named EnemyAI and write the following code:

using UnityEngine;

public class EnemyAI : MonoBehaviour


{
public Transform pointA;
public Transform pointB;
public float speed = 3f;
public float detectionRange = 5f;
public Transform player;

private bool movingToB = true;

void Update()
{
// Patrol between pointA and pointB
if (movingToB)
{
transform.position = Vector3.MoveTowards(transform.position,
pointB.position, speed * Time.deltaTime);
if (transform.position == pointB.position) movingToB = false;
}
else
{
transform.position = Vector3.MoveTowards(transform.position,
pointA.position, speed * Time.delta

You might also like