ARVR Unity Programs-For Manual
ARVR Unity Programs-For Manual
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.
Steps:
using UnityEngine;
Objective: Change the color of an object when the user clicks on it.
Steps:
using UnityEngine;
void Start()
{
rend = GetComponent<Renderer>();
}
void OnMouseDown()
{
rend.material.color = new Color(Random.value, Random.value,
Random.value); // Set to random color
}
}
using UnityEngine;
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);
}
}
}
Objective: Create a first-person character controller where the user can move around using
keyboard input and look around using the mouse.
Steps:
using UnityEngine;
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);
Objective: Create a simple racing game with a controllable car and a camera that follows the
car's movement.
Steps:
using UnityEngine;
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);
}
}
using UnityEngine;
void Update()
{
transform.position = target.position + offset;
transform.LookAt(target);
}
}
Objective: Create a basic 3D platformer where the player can jump and collect items.
Steps:
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);
}
}
}
using UnityEngine;
Objective: Create a simple enemy AI that patrols between points and attacks the player when
in range.
Steps:
using UnityEngine;
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