In this lesson we will learn how to use Unity and C# programming to make a 3D game with a rolling ball as the player and cubes that we can pick up to increase our score!
This light illuminates our scene from the opposite angle of our main light, giving the backside of the ball a nice hue.
To change the color of the light, click on the area next to the color property.
Feel free to adjust any of these values as you see fit, or change the fill color. Light the scene the way you want!
Adding a Rigidbody component to an object will put its motion under the control of Unity's physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions with incoming objects if the right Collider component is also present.
Unity Documentation: Rigidbody
Fill in your script with this code. Read the comments and think about what everything is doing.
Unity Documentation: Input.GetAxis
Unity Documentation: Rigidbody.AddForce
using UnityEngine; using System; public class PlayerController : MonoBehaviour { public float speed = 800.0f; void FixedUpdate() { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime); } }
Prefabs are a powerful tool in Unity. The prefab acts as a template from which you can create new object instances in the scene. Any edits made to a prefab asset are immediately reflected in all instances produced from it but you can also override components and settings for each instance individually.
Unity Manual: Prefabs
Unity Documentation: OnTriggerEnter
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "PickUp") { other.gameObject.SetActive(false); } }
1. Click on the box of anchor presets
2. Select "top left"
3. Adjust the X and Y position
For more information on Unity's UI system click here!
You may be wondering about the difference between a "float" and an "int"
They are both data types that store numbers, but there is a key difference:
This distinction is important because the two types are incompatible when it comes to mathematical operations. For example, if we try to multiply an int by a float, we get this error:
Teach coding to your students with MVCode Teach
MVCode offers an integrated product designed to teach coding to students.