OnTrigger and RaycastHit

Hi

I am using a trigger collider to check if my camera is colliding with any objects and I want to then have it move around the object.

I am using a modified version of the MouseOrbit script. I have tried to use OnCollision to get the extra info but it just sends my camera into a crazy spin.

Here is what I have so far:

void OnTriggerEnter(Collider other)
{
	RaycastHit hit;
	
	float offset = 0.5f;
	
	Vector3 collDirection = other.transform.position - transform.position;
	float collDistance = Vector3.Distance(transform.position, other.transform.position);
	
	Debug.DrawRay(transform.position, collDirection, Color.yellow);
	if(Physics.Raycast(transform.position, collDirection, out hit, 1))
	{
		Debug.Log(hit.distance);
		if(hit.distance < offset)
		{
			Debug.Log("Collision - Direction: " + collDirection + " - Distance: " + collDistance);
			
			desiredDistance = Mathf.Lerp(currentDistance, hit.distance + 0.1f, 1);
		}
	}
}

The problem is that it never registers the RaycastHit although sometimes it randomly comes up.

If someone could give me a hand with this or maybe even a different more efficient way then I would be very grateful, thankyou.

3d Buzz has a decent camera system tutorial. It uses the camera clip points plane to detect collissions. You need to check the whole area of the image in the viewport, because if you do a single point raycast then parts of the clip plane of the camera can go outside the object.

Thank you for that link I will have a look over them when I get the chance.

Something I should have mentioned in the OP is that I am doing this camera controller for a space game.

If anyone else has some input in the meantime just throw me a line. :slight_smile: