Rigidbody vs TranslateWhich Should You Use?

In this video I go over a few methods on how to move a player or an object around the screen. I think share my thoughts on which method is best for each use.

In this video I discuss:

  • 0:28 - Moving An Object Using Translate
  • 2:20 - Problems with using Translate
  • 2:50 - Using RigidBody for Physics & Collisions
  • 5:00 - 3 Ways to Move An Object with RigidBody
  • 5:10 - Using AddForce()
  • 6:15 - Using Velocity
  • 7:05 - Using MovePosition

 

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));
}

LINK TO THIS POST

SHARE THIS POST


DISCUSSION