I’m having trouble with something I would think should be really simple and it’s driving me crazy…
I am just trying to zoom the camera on the FPC and it’s not working correctly at all. I zoom in and out and it’s changing X values which I don’t even touch and moving other values to odd numbers etc…
var target : Transform;
private var targetZoomInMax : float = 2.0;
private var targetZoomOutMax : float = 5.0;
//private var targetForwardMax : float = 500.0;
//private var targetBackMax : float = 503.0;
function Start ()
{
//targetZoomInMax = target.position.y;
}
function Update ()
{
// Mouse wheel moving forward
if(Input.GetAxis("Mouse ScrollWheel") > 0 target.position.y > targetZoomInMax)
{
target.position += Vector3(0,-1,1);
print("Wheel Forward" + target.position);
}
// Mouse wheel moving backward
if(Input.GetAxis("Mouse ScrollWheel") < 0 target.position.y < targetZoomOutMax)
{
target.position += Vector3(0,1,-1);
print("Wheel Back" + target.position);
}
}
Can someone give me some tips?
As soon as I start moving the object around the screen seems to be when random values start getting added to X and Z.
Hi kingdbag
I think I must have misunderstood something, because you are changing the position based on the mouse wheel.
For a zoom effect you’ll want to change the field of view, her is how we do it:
C#
private Camera cameraFreeWalk;
public float zoomSpeed = 20f;
public float minZoomFOV = 10f;
public void ZoomIn()
{
cameraFreeWalk.fieldOfView -= zoomSpeed/8;
if (cameraFreeWalk.fieldOfView < minZoomFOV)
{
cameraFreeWalk.fieldOfView = minZoomFOV;
}
}
3 Likes
Yea, I worded that wrong… I am trying to go from first person to third person and vice versa.
Ah well that explains that 
Are we talking about a transition effect from 1st person controller to 3rd or a zoom effect in 3rd person?
The transition can be accomplished by lerping from one position to the other.
The 3rp person zoom should be calculated from the view direction.
I’m guessing it would be more of a transition effect. I just want to be able to zoom the camera out a few clicks from the FPC object so its a little bit in front of the camera and you can see the character.
I will have to look into lerping when I get home and see if I can figure it out! 
I really need some pointers because I am totally stuck. I guess I’m not understanding the whole transform parent/child association with what I’m trying to do.
All I want to do is when I mouse wheel back move the FPC camera only Y value from 1.0 to 4.0 and Z back from 0.0 to 4.0. I have this script on the Main Camera. Now I have tried the code about 15 different ways so this is the last thing I’ve been messing with.
So move mouse wheel back change from (0,1,0) to (0,4,4) and move forward mouse wheel change back to (0,1,0)
private var minimumY : float = 1.0;
var maximumY : float = 4.0;
var zoomZ : float = 4.0;
function Start ()
{
print("Start position" + transform.position);
}
function Update ()
{
// Mouse wheel moving forwards
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
//transform.position.x = 500;
transform.position.y = Mathf.Lerp(maximumY, minimumY, Time.time);
transform.position.z = Mathf.Lerp(transform.position.z, transform.position.z + zoomZ, Time.time);
print("Wheel Forward" + transform.position);
}
// Mouse wheel moving backwards
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
//transform.position.x = 500;
transform.position.y = Mathf.Lerp(minimumY, maximumY, Time.time);
transform.position.z = Mathf.Lerp(transform.position.z, transform.position.z - zoomZ, Time.time);
print("Wheel Backward" + transform.position);
}
}
What was happening before I was using lerps is still happening while using lerp… even if the FPC is standing still the camera starts to move all over the map even x is changing and im not even touching that… and it never aligns back up with the parent FPC… I am totally confused… any help? 
I took out some checks so I can mouse wheel back and forth unlimited amount of times this I know… that isn’t what I’m stuck on…
This is a tweak of the SmoothFollow camera script to allow zooming in and out…
/*
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;
var mouseWheelDistanceRate = 5;
var mouseWheelHeightRate = 5;
// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow With Zoom")
function LateUpdate () {
// Early out if we don't have a target
if (!target)
return;
if (Input.GetAxis("Mouse ScrollWheel") != 0) {
Debug.Log(Input.GetAxis("Mouse ScrollWheel"));
distance -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelDistanceRate;
height -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelHeightRate;
}
// 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);
}
[/code]
So I’m sorry to necropost this topic but after searching a programmer friend helped me find the solution for this, and I thought I’d post this instead of creating a brand new topic…
I don’t know how the first person controller works, and this topic is 7 years old so it’s probably improved greatly, but all you have to do to add mouse wheel zoom in and out on the Free Look Cam prefab is go into the FreeLookCam script and put this in it’s LateUpdate() { } or Update() would work as well I’m sure:
float mouseScrollSpeed = 5f;
m_OriginalDist -= Input.GetAxisRaw("Mouse ScrollWheel") * mouseScrollSpeed;
Here’s the code I used inside of FreeLookCam.cs:
//define this part:
private float m_OriginalDist;
public float m_ZoomOutMax = 5.5f;
//then put this in the start method:
m_Cam = GetComponentInChildren<Camera>().transform;
m_Pivot = m_Cam.parent;
m_OriginalDist = m_Cam.localPosition.magnitude;
//then put this in an update or lateupdate method:
if (m_OriginalDist > m_ZoomOutMax) {
m_OriginalDist = m_ZoomOutMax;
}
float mouseScrollSpeed = 5f;
m_OriginalDist -= Input.GetAxisRaw ("Mouse ScrollWheel") * mouseScrollSpeed;
You’ll then also want to probably turn clipping off if you zoom out too far from the character or something. Either way, that’s how you do it.