Code Due Apr 24
Hello Unity: Move the Cube
Unity Basics · 15 points
Instructions
Write a MonoBehaviour that moves a cube to the right at a constant speed. Use Time.deltaTime so it's frame-rate independent.
Your code
PlayerController.cs
| 1 | using UnityEngine; |
| 2 | |
| 3 | public class PlayerController : MonoBehaviour |
| 4 | { |
| 5 | public float speed = 5f; |
| 6 | |
| 7 | void Update() |
| 8 | { |
| 9 | float h = Input.GetAxis("Horizontal"); |
| 10 | float v = Input.GetAxis("Vertical"); |
| 11 | |
| 12 | Vector3 move = new Vector3(h, 0f, v); |
| 13 | transform.Translate(move * speed * Time.deltaTime); |
| 14 | } |
| 15 | } |
(disabled in the demo)
Graded · 15/15
Clean work, Maya! Movement is smooth and you used Time.deltaTime correctly. Next step: try normalizing the move vector so diagonal movement isn't faster than straight movement.