Hi guys I make a videogame with a player in thirdperson , the camera movement depend of player , I have code (c#) for camera. This code detect if collides with a wall , I have the validation but I do not know what to do when the if or the else be fulfilled , some idea ? Thanks guys!
this is the code : `
if (Physics.Raycast (transform.position, backWard, distanceAway))
// do somethings
// do somethings
What do you mean by validate camera?
A couple of different ways to accomplish this, but none of them are perfect. Solution 1 is, as Tpizzle13 says, fire raycasts from the camera to the player each frame, and move forward when something is hit that keeps it from reaching the player. You can’t really account for the thickness of an object, so you’ll likely need to keep firing raycasts / moving forward until you can hit the player with the raycast again, and then you’re fine. You can smooth this out by setting camera destination targets and lerping between them, so it’s not jumpy / jittery and the camera just smoothly tries to keep the player in view.
It’s worth noting that, for raycasts, you should be really specific about the layers of objects that count as “obstacles”, so you aren’t constantly moving forward in response to hitting special effects or small desks / whatever that only partially block the view and aren’t “walls”.
The second option is to use a collider on the camera, and the rest is more or less how the raycast method works, but inside of OnCollisionEnter/OnCollisionStays instead. Just keep moving the camera forward until it’s no longer colliding, and you should be more or less okay- use camera destination targets and lerp in order to smooth things out.
If you want to be fancy and do a bit of studying, you can also use the Cinemachine system in the UAS. It already has functionality built-in to do exactly this, but it’s a complex system and takes some time to get used to. There are likely a thousand other assets both on the UAS and shared freely on the web that can also fulfill the specific need you have here, but Cinemachine is a top-tier, phenomenal extension, and free, so I’d say give some tutorials a whirl for that.