there are two way to control object movement:
- By directly manipulating the transform of a object
using UnityEngine; using System.Collections;
public class movementControl : MonoBehaviour {
public float moveSpeed = 10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}
}
- By using physics Rigidbody add force
using UnityEngine; using System.Collections;
public class moveControl : MonoBehaviour {
private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis ("Vertical");
rb.AddForce (x, 0, y);
}
}