- Home /
How to make a camera follow the mouse up and down?
I'm making a game in which a 1st and 3rd person camera that is parented to a tank follow the mouse around in the x and z axis. The script is the basic script included with unity: This camera smoothes out rotation around the y-axis and height. Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value. Then we smooth it using the Lerp function. Then we apply the smoothed values to the transform's position. */
// The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu @script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () { // Early out if we don't have a target if (!target) return;
// Calculate the current rotation angles
wantedRotationAngle = target.eulerAngles.y;
wantedHeight = target.position.y + height;
currentRotationAngle = transform.eulerAngles.y;
currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
} A bullet with the script:
var colision = 0; var bulletspeed = 400; var bullet : Transform; function Update () { if(Collision){ if(colision == 3){ } else{ colision +=1; rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletspeed)); } } else{ rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletspeed)); } }Is fired in the direction the camera is looking at. I was wondering what I would need to add in to make the camera also move up and down in the y axis so I could set the elevation of a shot. Cheers
Answer by aldonaletto · Mar 05, 2012 at 11:47 PM
The best solution is to create an empty object and child the camera to it. Reset the camera's position and rotation, remove SmoothFollow and attach the MouseLook.cs script instead, then select MouseY in the MouseLook Axes field in the Inspector. Finally, attach the SmoothFollow script to the empty object: this way, the empty object and the camera will follow the target, while MouseLook will control the camera only in the up/down direction.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064810im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make my bullets spread when fired 3 Answers
scripting question 3 Answers
Turret bullet rotation problem 1 Answer
Fast bullet ignor collision sometimes 3 Answers
Turn off function 2 Answers