Collision causing unwanted movement

So basically I’ve been going through every option on this forum as to how collision detection for my player character but when I collide with a object my character goes crazy and starts moving (on all axis) by itself. If I move the player with the keys and then let go it continues to move by itself. I’m also trying to keep my character at a y level of 1.

The self movement started occurring after I added a rigidbody to detect collisions.
The game is a overhead game. (Overhead like GTA 2)

This is my PlayerMovement script

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {

	private float forwardSpeed = 1.4f;
	private float backwardSpeed = 1.2f;
	private float runSpeed = 0.6f;
	private float speed;
	private float hitdist;
	private Transform myTransform;
	private Ray ray;
	private Vector3 targetPoint;
	private Quaternion targetRotation;
	
	void Start() {
		myTransform = transform;
		
		speed = forwardSpeed;
	}
	
	void Update () {
		// Character movement (Forward/Backwards)
		if (Input.GetAxis ("Move") > 0) { // Forward
			// Running
			if (Input.GetButton ("Run")) {
				speed = runSpeed;	
			} else {
				speed = forwardSpeed;
			}
			
			myTransform.position += myTransform.forward * speed * Time.deltaTime;
		}
		
		if (Input.GetAxis ("Move") < 0) { // Backward
			speed = backwardSpeed;	
			
			myTransform.position += -(myTransform.forward) * speed * Time.deltaTime;
		}
		
		
		// Rotates the player to face the mouse
		Plane playerPlane = new Plane(myTransform.up, myTransform.position);
	    ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	    hitdist = 0.0f;
	    if (playerPlane.Raycast (ray, out hitdist)) {
	        targetPoint = ray.GetPoint(hitdist);
	        myTransform.rotation = Quaternion.LookRotation(targetPoint - transform.position);
	    }
	}
	
	void OnCollisionEnter(Collision col) {
		switch (col.transform.tag) {
			case "Wall":
				myTransform.position = new Vector3(myTransform.position.x, 1, myTransform.position.z);
				break;
			default:
				myTransform.position = myTransform.position;
			break;
		}
	}
}

Any suggestions on why it is moving itself? Or better collision detection?

OnCollisionEnter is best used with a Rigidbody, not a mathematical movement. I believe you need to consider using a CharacterController. This will give you the movement types you seek, and still retain mathematical movement.

I know this issue was in 2012. Hoping this will still help someone facing a similar issue.

I had this same problem just a day ago and went through forums. Found out that when colliding with obstacles (non-rigidbody), the physics causes the transform to angle differently from the desired and makes the movement run away. My solution was to freeze one of the axis of the transform (for me it was the y-axis for height). Now it works as desired.

However, sometimes after collision, the transform will do an undesired rotation non-stop. This was corrected by increasing the angular drag of the rigidbody.

1 Like