How to Move Cube in Unity Left ,Right, Up, Down Script C#

Here is a script for getting a cube to move around.

using System.Collections;
using UnityEngine;
public class Cube : MonoBehaviour 
{
  public float speed; 
// Use this for initialization 
void Start ()
 { 
   speed = 10f; 
 } 
// Update is called once per frame 
void Update () 
{ 
    if (Input.GetKey (KeyCode.W))  
    { 
        transform.Translate (Vector3.forward * Time.deltaTime * speed); 
    } 
    if (Input.GetKey (KeyCode.S))  
    { 
        transform.Translate (-1 * Vector3.forward * Time.deltaTime * speed); 
    } 
    if (Input.GetKey (KeyCode.A))
    {
        transform.Translate ( Vector3.left * Time.deltaTime*speed);
    }
    if (Input.GetKey (KeyCode.D))
    { 
        transform.Translate (-1 *Vector3.left * Time.deltaTime * speed); 
    }
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *