Source Code for Translate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class translate : MonoBehaviour {
public float speed = 10.0f;
// Update is called once per frame
void Update () {
moveCharacter(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")));
}
void moveCharacter(Vector2 direction){
transform.Translate(direction * speed * Time.deltaTime);
}
}
Source Code for RigidBody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rbmovement : MonoBehaviour {
public float speed = 10.0f;
public Rigidbody rb;
public Vector2 movement;
// Use this for initialization
void Start () {
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
void FixedUpdate(){
moveCharacter(movement);
}
void moveCharacter(Vector2 direction){
//insert one of the 3 types of movement here
}
}
To Move With AddForce()
void moveCharacter(Vector2 direction){
rb.AddForce(direction * speed);
}
To Move With Velocity
void moveCharacter(Vector2 direction){
rb.velocity = direction * speed;
}
To Move With MovePosition
void moveCharacter(Vector2 direction){
rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
}