How to make a camera that supports collision limitation?

I’m currently trying to make a game where you control a ball, which rolls relative to the direction the camera is facing like a third person game. I have all of what I can see being needed done, but I can’t for the life of me figure out how to make it so that the camera can’t go inside of parts with rigidbodies or collisions. Here’s the script, the area I’m trying to work with is mainly lines 282-294, in which I tried limiting the min and max distance as there is a bug where it is always the minimum, even if you try to zoom out (only happens with a larger max distance). ball thing? - PLAYCANVAS

You will also notice that even if the camera isn’t in a rigid body it shrinks, and doesn’t when in some rigid bodies.

Thanks in advance for the help!

Hi @Cryptonaph and welcome,

Check the third person example project, it handles camera collisions with other rigid bodies:

https://developer.playcanvas.com/en/tutorials/third-person-controller/

Hi @Leonidas, I would totally use some parts of this but I’m still getting the hang of how the script components work and how raycasting works, in fact I tried using that script on the ball but there is one problem with doing so and that is the fact that the axis point is tied to the ball, and so since the ball moves using impulse forces and the physics engine the camera axis point rotates when the ball rolls, causing the camera to behave in a way that I don’t want it to behave in. Is there any way that I can essentially make it into a ball socket where the point stays at the center of the ball, doesn’t rotate, but the ball still does?

Nevermind, I found a way around, but if there is a better way than this, let me know:

  • Get position of ball
  • Keep Camera Axis parented under “scene” instead of “ball”
  • Have script constantly move Camera Axis to the position of ball, preventing orientation change

@Leonidas Sorry to ping you, but a new problem has risen, and that problem would be the fact that the ball no longer orients itself according to the camera. I’m not sure what to do about this, could I get some help with this? I’ve tried manipulating the ball’s rotation, using setRotation(), setLocalRotation(), setEulerAngles(), and setLocalEulerAngles(). Doesn’t seem like it wants to work. The ball moves based on its vector3 front, applying an impulse to push it, so for the player to control it properly the ball always has to face away from the camera.

Nevermind, I fixed it, but yet again, new problem. The texture doesn’t rotate like it should and I’m not sure how to fix that.

After a lot of studying, I figured out how to fix it all! Hopefully I shouldn’t have to bring this post back. Thanks for the help!

1 Like