this is a bit of a tough nut to crack and no approach has lead me to a satisfying result so far.
So here’s the thing:
I want my camera to follow a ball (controlled by physics) thats rolling down a winding tube . Since the ball is spinning wildly I cannot attach the cam to the ball.
I tried to constrain some null-object to the position of the ball and attach the cam to that and that kinda works. The only problem is, that the camera is supposed to look down the tube, which is doesn’t do, because I don’t know how to calculate the direction the camera should aim to.
My best guess so far was to use the direction the ball is currently rolling to. That works, but the camera tends to go through the walls a lot.
Are there maybe some ideas to improve the camera control?
never tried anything like this but perhaps if you attach your camera to a second ball controlled by physics (exactly as the first one but a little behind) and you freeze the rotation of the second ball (there is a checkbox) then your camera shouldn’t spin. I think you can also use lookAt to point the camera in the right direction (to look at the ball).
You can also try storing the position of the physics ball. Say where the ball was a few frames back and then assign them to the camera. The camera should be looking at the next position.
Finally, there might already be a script that comes with unity that does more or less what you want. Check the standard assests first for a smooth follow script.
you could try making the ball a child of a null object. the parent does not spin, but the ball does, so you could attach the camera to the same parent without worrying about the spin. hope this helps.
I’ve attached a project with a physics-controlled camera that might be useful to you here. Rather than parenting the camera with the ball or creating a second dummy object to follow the ball, it is often better to apply forces directly to the camera. This particular camera uses a virtual “spring” to keep it a certain distance from the target object, and other springs to make it hover over the ground and push it away from surrounding walls and obstacles. The code is commented and (I hope) reasonably easy to understand, but please ask if you have any trouble with it.
Incidentally, the spring is implemented as a separate class which is useful in its own right.
I’ve got a problem with this springcam example.
I moved every object (except for the Plane) 7 units up in Y direction. The problem now is, that the camera isn’t always hovering above the ground.
Furthermore I tried to use this camera in my project. The problem I have is that the camera looks in the wrong direction. Is there a possibility to achieve that the camera is always behind the player? I think I have to implement it on my own, right?
ive set up my camera exactly the way it is in the example scene, but when i run my game, even when my ball isnt moving, the camera is still moving around. none of the balls transform values are changing, but the camera is still trying to rotate…
Is this causing a visual problem? The camera is controlled by physics, so it will tend to drift slightly even when the target object has stopped moving. You can increase the drag on the camera’s rigidbody to reduce this effect if necessary.
now that my camera set up has changed, how do i change the control set up?
before, my camera was just a static camera that just looked at the marble with an offset.
but because now the camera moves around, the controls seem really funny, especially when the camera faces the opposite direction of the initial static camera, because the controls are all world set x,y,z controls.
how do i set it up so that the controls will be relative to the what the camera is looking through…
of the HoverCam script posted above. i’ve assigned an object for the cam to follow and as far as i can see i’ve set things up in my scene the way it’s done in the above demo, but no dice. Any ideas?
There isn’t any object referenced in that line, so you may be getting a misleading error message. However, firstly check that the error is referring to the file you think it is (use the console window to see more detail about the error if you are not already doing so). If this is OK, then please post a few lines of context from the script around the line that is being reported as causing trouble.
Thanks for the spring script, although I couldn’t get them to work.
I’m also making a ball game and I have specific things I want the camera to do I used the HoverCam and Spring scripts in my project, but the camera just follows the ball without springing at all (values are set to Base Force 0, Reaction 1, Ideal 5, Damping 1, and I froze the rotation of my ball.)
What I really want is a camera that will follow my ball through a loop, just like a racecar game (see Jet Car Stunts for a good example.) I tried doing a raycast “down” from the camera’s local y-axis to check the height of the camera from the track, and this works great. The problem is that I can’t modify the distance from the ball correctly (I want the camera to try to be 10 units away from the ball, while still staying 3 units away from the ground at all times. Meaning, the local Z direction of the camera, which is looking at the ball, should have exactly 10 units between it and the ball in that direction, but I can’t modify the local Z direction of the camera, just the global direction!)
So frustrating! If someone has a racecar cam-follow example that will follow a car through a loop, this would work for me! And possibly for the thread-starter as well!