collision between player and wall help.

Hi,
I’m sure i’m the 10000 guy ask this question, i’ve search long time and i don’t find concrete help for this basic stuff,
surely noob question but how my player can detect wall and correctly collide with it ?

I’ve set up camera controller and chracter controller move by click, i place a basic cube collider and my character controller named ‘player’ (a capsule collider for test) going trough this ‘wall’ .

My idea is to stop the character controller face to the wall when player click through, maybe it’s not physic or not need a real collision between them.
Script MoveOnMouseClick

public class moveOnMouseClick : MonoBehaviour {
	private Transform myTransform;				// this transform
	private Vector3 destinationPosition;		// The destination Point
	private float destinationDistance;			// The distance between myTransform and destinationPosition
 
	public float moveSpeed;						// The Speed the character will move
 
 
 
	void Start () {
		myTransform = transform;							// sets myTransform to this GameObject.transform
		destinationPosition = myTransform.position;			// prevents myTransform reset
	}
 
	void Update () {
 
		// keep track of the distance between this gameObject and destinationPosition
		destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
		if(destinationDistance < .5f){		// To prevent shakin behavior when near destination
			moveSpeed = 0;
		}
		else if(destinationDistance > .5f){			// To Reset Speed to default
			moveSpeed = 10;
		}
 
 
		// Moves the Player if the Left Mouse Button was clicked
		if (Input.GetMouseButtonDown(0) GUIUtility.hotControl ==0) {
 
			Plane playerPlane = new Plane(Vector3.up, myTransform.position);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			float hitdist = 0.0f;
 
			if (playerPlane.Raycast(ray, out hitdist)) {
				Vector3 targetPoint = ray.GetPoint(hitdist);
				destinationPosition = ray.GetPoint(hitdist);
				Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
				myTransform.rotation = targetRotation;
			}
			
		}
 
		// Moves the player if the mouse button is hold down
		else if (Input.GetMouseButton(0) GUIUtility.hotControl ==0) {
 
			Plane playerPlane = new Plane(Vector3.up, myTransform.position);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			float hitdist = 0.0f;
 
			if (playerPlane.Raycast(ray, out hitdist)) {
				Vector3 targetPoint = ray.GetPoint(hitdist);
				destinationPosition = ray.GetPoint(hitdist);
				Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
				myTransform.rotation = targetRotation;
			}
		//	myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
		}
 
		// To prevent code from running if not needed
		if(destinationDistance > .5f){
			myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
		}
	}
}

and Camera Controller :

public class CameraController : MonoBehaviour 
{
	// Camera's target	
	public GameObject target;
	
	// The offset to position the camera in the right spot
	public float offsetX = -15;
	public float offsetZ = -15;

	// The maximum distance for the camera.
	// It's used for easing the movement
    public float maximumDistance = 2;

	// The speed to reach the player
	public float speedToTarget = 10;
	
	
    public void LateUpdate() 
    {
		// Move the camera to right place
        Vector3 targetPos = target.transform.position;
        Vector3 myPos     = transform.position;
        
        float movX = (targetPos.x + offsetX - myPos.x) / maximumDistance;
        float movZ = (targetPos.z + offsetZ - myPos.z) / maximumDistance;
		
        myPos.x = movX * speedToTarget * Time.deltaTime;
        myPos.z = movZ * speedToTarget * Time.deltaTime;
        myPos.y = 0;
        
		transform.position += myPos;		
		
			}
		}

Thank you a lot !!!

well there are two approaches… you can design your own collision detection code (not exactly newbie friendly i don’t think, and there are lots of ways of doing this), or you can add in a rigidbody component and have that handle collision for you (loads about this on the forums and script reference).

Personally I’d advise looking up the rigidbody references; quick tip, you’ll run into issues with things if you do “transform.position = …” updates on a gameObject with a rigidbody attached, you can either handle them (zero off the rigidbody.velocity) or change the movement code to something like “transform.rigidbody.velocity = …”

Thank you i’ll try :wink:

There is also a Character Controller component which probably does exactly what you want. Just add it to your player (Component->Physics->Character Controller) and use the Move method… (see documentation)