Looking for a fix to my problem regarding player collisions and movement...

Hey all, I am trying to make a very simple game with my player being a cube, through some digging online I have found some code that works pretty great for movement, but I keep having one main problem:

Whenever my player cube runs into a wall, it sort of jitters through the wall and bounces around all crazy.

I have a rigidbody component on the player along with a box collider, but no amount of editing can seem to solve this for me. Any suggestions on a fix? Or where to go from here?

30 sec Video reference of my problem:

A screenshot of my player’s components: Imgur: The magic of the Internet

The code I am using for movement:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float speed;

    void Update()
    {
        Vector3 moveDir = Vector3.zero;
        moveDir.x = Input.GetAxis("Horizontal") * speed; // get result of AD keys in X
        moveDir.z = Input.GetAxis("Vertical") * speed; // get result of WS keys in Z
        // move this object at frame rate independent speed:
        transform.position += moveDir * speed * Time.deltaTime;
    }
}

You do not want to modify the transform position of an object that has a rigidbody unless it is kinematic. Basically, a rigidbody should handle the positioning of itself through unity’s physics system during FixedUpdate. When you start modifying the transform position, the physics system is still trying to do its positioning of the object.

If you want to move a rigidbody you have to modify its velocity, and then the physics system will move the object during FixedUpdate.

The kinematic checkbox means the physics system won’t bother trying to position an object, but it also means your gonna have to do your own collision testing stuff.

Replace line 15 with :

GetComponent<Rigidbody>().AddForce(moveDir * speed * Time.deltaTime);

Hike your speed up to about 30-35, and in your rigidbody component under constraints Freeze Rotation on X,Y, and Z unless you want box rolling all over the place.