Table of contents
Step-by-step tutorial
Create a Plane
GameObject using the Hierarchy window create menu:
Create a Sphere
GameObject using the Hierarchy window create menu:
Both game objects should be now positioned in the center of the 3D space (0, 0, 0):
You can reset an object position using the Transform
component's "gear" menu in the Inspector window:
Select the Sphere
game object in the Hierarchy window and lift it up by setting the Y Position of its Transform
component to 0.5
:
Let's color the ball so it's easier to see it.
Create a new folder called Materials
in the Assets folder. Create a new material called Pink
inside it (using the Project window Create menu):
Select the Pink
material in the Project window and set its color in the Inspector window. I'm using the hexadecimal value D61594
:
Drag the Pink
material on the Sphere
object:
You should now have a pink ball and a white plane below it:
(The orange borders in the Scene window shows up when you select the Plane
object in the Hierarchy window.)
Create a folder called Scripts
in the Assets
folder.
Create a C# script called Move
inside the Scripts
folder:
Drag the Move
script on the Sphere
object:
Select the Sphere
object in the Hierarchy window and you should see the Move
script in the Inspector window:
Click the Add Component
button in the Inspector window to add a new component to the Sphere
object.
Add a Rigidbody
component:
Disable the Plane
game object temporarily by ticking off the checkbox next to its name in the Inspector
window and you will see the ball falling down.
Double-click the Move
script in the Project window to open it.
Add the following code to it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
// HERE
private Rigidbody rb;
void Start()
{
// HERE
rb = GetComponent<Rigidbody>();
}
void Update()
{
}
// START
void FixedUpdate()
{
rb.AddForce(Vector3.right);
}
// END
}
The rb
field holds a reference to the object Rigidbody
:
private Rigidbody rb;
In the Start()
function we get the Rigidbody
component attached to the object using the GetComponent<type>()
function:
rb = GetComponent<Rigidbody>();
In the FixedUpdate()
function we apply a force to the ball:
rb.AddForce(Vector3.right);
This tells the engine that we want to "push" the ball along the world space red axis. Vector3.right
is a shorthand for Vector3(1, 0, 0)
.
Click the Play
button and you should see the ball rolling along the world space x-axis.
Let's move the ball using user input. Edit the Move
script and add the following lines:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
private Rigidbody rb;
// START
private float horizontalInput;
private float verticalInput;
private float speed = 10.0f;
// END
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// START
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
// END
}
void FixedUpdate()
{
//rb.AddForce(Vector3.right);
// HERE
rb.AddForce(new Vector3(horizontalInput, 0.0f, verticalInput) * speed);
}
}
First we create fields to store user input data and speed of the movement:
private float horizontalInput;
private float verticalInput;
private float speed = 10.0f;
In the Update()
function we read the user input:
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
The Input.GetAxis
function returns a value between -1
and 1
. By default, "Horizontal" and "Vertical" axis are mapped to arrow keys.
In the FixedUpdate()
function we use the input data and speed to push the ball:
rb.AddForce(new Vector3(horizontalInput, 0.0f, verticalInput) * speed);
Move the ball around using arrow (or WASD) keys.
move.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Move : MonoBehaviour
{
private Rigidbody rb;
private float horizontalInput;
private float verticalInput;
private float speed = 10.0f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (Input.GetKeyDown("r"))
{
SceneManager.LoadScene("SampleScene");
}
}
private void FixedUpdate()
{
//rb.AddForce(Vector3.right);
rb.AddForce(new Vector3(horizontalInput, 0.0f, verticalInput) * speed);
}
}
Leave a comment