How to make object move in Unity

String header = "The post about coding :)";
Today I’ll tell about how to have the ship move by keyboard arrows.
First, add a new script:
![]() |
---|
Second, add this code in the script:
using UnityEngine;
public class PlayerCtrl : MonoBehaviour
{
public float Speed = 7f;
Transform Transf;
// Use this for initialization
void Start ()
{
Transf = GetComponent<Transform> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.LeftArrow)) {
Transf.position -= new Vector3 (Speed * Time.deltaTime, 0, 0);
} else if (Input.GetKey (KeyCode.RightArrow)) {
Transf.position += new Vector3 (Speed * Time.deltaTime, 0, 0);
}
}
}
What’s going on in this script? Let’s see.
void Start ()
– happens once, on object initialization.void Update ()
– happens every frame redraw. Means, as many times as your framerate is.Time.deltaTime
– keeps the delta time from the previous frame.