Unity 3D: How do I make any object move in Unity
Please Subscribe to our YouTube Channel
We are going to explain 2 ways to make the character move in unity. You can use any way you like the most.
How to make any Object Move in Unity
First of all, create the object as you always do in Unity. Create a C# Script and then drag the Script to the object. You can also watch the video to see the complete process. Don’t forget to subscribe to our YouTube Channel to learn more.
Method 1: Moving the Object using Arrow Keys
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController: MonoBehaviour
{
float speed = 5f;
void Update()
{
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
}
Method 2: Moving Objects With WASD or Arrow Keys
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour
{
float speed = 5f;
void Update()
{
transform.Translate(new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0f, Input.GetAxis("Vertical") * speed * Time.deltaTime));
}
}