can anyone help with vall follow camera

i am pulling my hair with this, i have a rolling ball that uses the arrow keys for movement, i need a camera that rotates around the ball when the ball turns so that the camera is always behind the ball, the camera needs to bank to the side when the ball turns left or right then slowly rotate back when the ball starts rolling straight again.

i have exhausted all the scripts i can find and have tried to adjust some of them but to no avail, it just will not work.

help appreciated

can anybody help me with this ?

to do this, you will need to write a follow script, which sets the camera at a fixed distance based on the balls velocity.

say your ball moves at 1,0.5, 1. First, remove the y, so you get 1,0,1, now reverse it to get -1, 0, -1. Normalize to make it a unit of 1. Now, take the normalized vector times the follow distance. Add a raw y of 1, so you get like -7.7, 1, -7.7 (if following at 10 units) add the balls position. This now gives you the position of the camera. (or where you want the camera to be) Now, simply lerp that position over time (camera.position = Math.Lerp(camera.position, newPosition, 5 * Time.deltaTime). This will move the camera over time so it doesn’t seem so jerky.

OK, now for the last good bit. During this calculation, you will need to figure out the orientation of the motion. For simplicity’s sake, try something like this:

var cameraTarget = (rigidbody.velocity * new Vector3(-1, 0, -1)) .normalized * 10 + transform.position;
var currentPosition = camera.position;
var position = camera.position;
position.y = transform.y;
camera.position = Vector3.Lerp(position, cameraTarget, 5, * Time.deltaTime);
camera.LookAt(transform.position);

// now, get the input
var input = new Vector3(-Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
var force = camera.TransformDirection(input) * 100;
rigidbody.AddForce(force);

// now, with the force applied, move the camera up.
camera.position = camera.position + Vector3.up;
camera.LookAt(transform);

Of course its loose, and It kind of follows what I remember of moving stuff around, but its a start. :wink:

is that in javascript as i need c# please

I believe it’s both. C# and javascript. If you are asking me to write all of your code for you, no, you need to learn how to use, and read code and translate it for your use. All languages are effectively the same, as long as you know how to translate them. :wink:

1 Like

Unity Standard Assets example project has some good camera scripts and techniques that will do what you want.