0% found this document useful (0 votes)
2 views7 pages

Simple Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Simple Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

A.

Moving the cube

First, create a flat surface and a green texture. Then, create a cube.

Now, lets create a new script.

● Inside Assets folder from the Project window, create a new folder called Scripts.
● Go inside the Scripts folder, then Right click > Create > C# Script.
● Change the name to CubeController.
● Write down the following code and save it.
● Finally, attach the script to the Cube gameobject.

CubeController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeController : MonoBehaviour


{
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{

float speed = 5.0f;

float horizontal = Input.GetAxis("Horizontal");


float vertical = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(horizontal, 0, vertical);

transform.Translate(direction * speed * Time.deltaTime);

}
}

Play the game.


You should be able to move the cube based on your controls.

B. Hello Earth!

Create a sphere and add an earth texture to it.


Download this texture:
https://www.solarsystemscope.com/textures/download/2k_earth_daymap.jpg
Now, create another script named RotateSphere and include the following code.

RotateSphere.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateSphere : MonoBehaviour


{
// Rotation speed in degrees per second
public float rotationSpeed = 30f;

void Update()
{
// Apply horizontal rotation (around the Y-axis) to the sphere
transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
}
}

C. Box Collider

Now, add a new tag named Cube and assign it to the Cube gameobject.
To do that, select the Cube in the hierarchy.
Now, In the inspector, click Untagged and select Add Tag. Refer to the screenshots below.
Click on the Plus (+) sign.

Add a new tag name Cube.

Attach this tag to our cube.


Also, add Sphere tag for the sphere and attach this tag.
Now, add a Sphere Collider to the our sphere (earth).
To do that, select the sphere (Earth) in the Hierarchy and ensure it has a SphereCollider
component.
Component > Physics > SphereCollider.

Now, add a Box Collider to our cube.


To do that, select the cube in the Hierarchy and ensure it has a BoxCollider component.
Component > Physics > BoxCollider.

Finally, Add a Rigidbody component to one or both objects. You can simply add Rigidbody
component to the sphere (earth).

Select the sphere. Go to Component > Physics > Rigidbody.

Finally, create a new script named CollisionHandler and attach it to both sphere (earth) and the
cube.

CollisionHandler.cs

using UnityEngine;

public class CollisionHandler : MonoBehaviour


{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Sphere") && gameObject.CompareTag("Cube"))
{
Debug.Log("The asteroid has hit the earth!");
}
}
}

Run the game. You should now be able to see that your cube can hit the earth and a message
appears upon hitting it.
”The asteroid has hit the earth!”

[BONUS]

Can you try this?

A. Bounce the earth

1. Attach Rigidbody to the Sphere:


• Select your sphere in the Unity hierarchy.
• Go to the Inspector window and click on “Add Component.”
• Add a “Rigidbody” component to the sphere, which will allow it to interact with Unity’s physics
system.
2. Create a C# Script:
• Right-click in your Project window (under “Assets”), select Create > C# Script, and name it
something like SphereBounce.
• Double-click the script to open it in your preferred code editor.
3. Write the Bouncing Script:
• Add the following code to your SphereBounce.cs script:

using UnityEngine;

public class SphereBounce : MonoBehaviour


{
public float bounceForce = 10f; // Amount of force applied for the bounce
private Rigidbody rb;

void Start()
{
// Get the Rigidbody component attached to the sphere
rb = GetComponent<Rigidbody>();
}
void Update()
{
// Check if the space key is pressed
if (Input.GetKeyDown(KeyCode.Space))
{
// Apply an upward force to the sphere
rb.AddForce(Vector3.up * bounceForce, ForceMode.Impulse);
}
}
}

4. Attach the Script to the Sphere:


• Drag the SphereBounce script onto your sphere in the hierarchy.
• In the Inspector, you should see the BounceForce field, where you can adjust the intensity of
the bounce.
5. Testing the Bounce:
• Press play, and then press the space key to see the sphere bounce upward.

You might also like