How to make camera collision with my custom camera?

Hello.
My camera is:
An empty object in the middle of the character,with y axis rotation script. The x axis rotation script is on the character, so when i rotate camera to left,char rotates too.On the empty object there’s a child - Main Camera with an offset,creating perfect third person camera, for my game. I tried adding rigidbody to it,a with sphere collider. Tried adding this script to Main Camera:

	private void CompensateForWalls (Vector3 fromObject, ref Vector3 toTarget) {
		Debug.DrawLine (fromObject, toTarget, Color.cyan);
		RaycastHit wallHit = new RaycastHit ();
		if (Physics.Linecast (fromObject, toTarget, out wallHit)) {
			Debug.DrawRay (wallHit.point, Vector3.left, Color.red);
			toTarget = new Vector3 (wallHit.point.x, toTarget.y, wallHit.point.z);
		}
	}

But it didnt help. It didnt even draw those lines. Any ideas how to make it collide with walls? Thx in advance.

Happy upcoming new year to everyone :slight_smile:

You have 2 lines there that should be drawn, if you see only 1 then i would add Debug.Log Code to ensure that the code is indeed executed correctly.

If you cyan line is still not visible you should check your Vectors.

My code would look like this :

    private Vector3 CompensateWalls(Vector3 input) {
    
    Debug.DrawLine (fromObject, toTarget, Color.cyan);
    RaycastHit wallHit = new RaycastHit ();
    
    if (Physics.Raycast (fromObject, toTarget, out wallHit)) {
		Debug.DrawRay (wallHit.point, Vector3.left, Color.red);
		Vector3 toTarget = new Vector3 (wallHit.point.x, toTarget.y, wallHit.point.z);
		return toTarget;
    }
	
	return input;
	}

Main issue is that you use linecast, not raycast i would assume.

A simple solution to achieve what you want – probably just make the collider on the camera a SPHERE, not a box. Say, a meter across (not tiny). That will make it just “stop” instead of jiggling around. Be sure to use the correct materials for both.

Really, you would perhaps be better to just use a trigger and control everything manually, but that will work fine if it’s just a “hello camera operations” exercise.