- Home /
How to move camera on local z axis
Im trying to make a camera script similar to Civilization 5. So far it works well until I add the Zoom() function. It interferes with the panning of the camera when moving forward and back, and its because both the Zoom() and the movement on the x of the camera are moving in the same direction. That's not what I want. I want the camera to zoom on its local axis, (localPosition and World position return same position). So I need a solution to make the camera move on its local z axis when using the scroll wheel.
function Zoom() {
if(Input.GetAxis("Mouse ScrollWheel") > 0) {
zoomTarget = transform.localPosition.z + speed;
}
transform.localPosition = Vector3(transform.localPosition.x, transform.localPosition.y,
Mathf.Lerp(transform.localPosition.z, zoomTarget, Time.deltaTime * 10 / lerpDamp));
}
$$anonymous$$hada showed you the answer. Don't mix up localPosition with the local space of your gameobject. localPosition is the position of your object in the local space of the parent object and not in it's own local space, which wouldn't make any sense.
Answer by Khada · Aug 17, 2012 at 07:46 PM
I'll explain with an example. You can adapt it to your own needs.
Say I want to move a camera forwards, in the direction of its local forward direction. I would do this:
Camera.main.transform.position += Camera.main.transform.forwards * Time.deltaTime * fMySpeed;
You can also use transform.back, transform.left, transform.right, transform.up & transform.down.
How can I do this smoothly? It works but its very jittery since its exact...
$$anonymous$$ove the code into FixedUpdate() ins$$anonymous$$d of Update() and replace deltaTime with fixedDeltaTime. That should smooth it a little, though I can make no guarantee.
Don't forget to mark the question as answered :)
Did that work? Don't forget to mark the question as answered.
Don't move that into FixedUpdate. That will make it worse. Also even in FixedUpdate you should use Time.deltaTime. It will return the fixed timestep inside FixedUpdate.
Actually I found an alternative. I'll post the code for those interested when I get back on my pc. I basically smoothDamped the field of view parameter, that way I don't have to worry about collision when zoo$$anonymous$$g.
Answer by kag359six · Aug 21, 2012 at 08:53 PM
Although Khada's answer is correct, I would like to share the alternative solution I've found in case anybody has the same issue. Here is the code for my zoom function:
function Zoom() {
var minZoom = 30;
var maxZoom = 70;
if(Input.GetAxis("Mouse ScrollWheel") > 0) {
var currentView = camera.fieldOfView;
targetView = currentView - 17;
}
camera.fieldOfView = Mathf.SmoothDamp(camera.fieldOfView, targetView, zoomVel, damp);
if(Input.GetAxis("Mouse ScrollWheel") < 0) {
currentView = camera.fieldOfView;
targetView = currentView + 17;
}
camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, minZoom, maxZoom);
}
Nice. Did you manage to smooth the camera movement or is this your work around? Just curious :)
Wouldn't it make more sense this way:
var $$anonymous$$Zoom = 30;
var maxZoom = 70;
private var targetView = maxZoom; // not zoomed at start
var zoomStep = 17;
function Zoom()
{
targetView -= Input.GetAxis("$$anonymous$$ouse ScrollWheel") * zoomStep;
targetView = $$anonymous$$athf.Clamp(targetView, $$anonymous$$Zoom, maxZoom);
camera.fieldOfView = $$anonymous$$athf.SmoothDamp(camera.fieldOfView, targetView, zoomVel, damp);
}
This is essentially the same, but it clamps the targetView and not the current one.
yeah it does thanks. First time using the scroll wheel for anything i didnt know you could use it like that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Need to limit zoom in/out of camera script 4 Answers
ScrollWheel Input reseting? 1 Answer
Camera Zoom Input change. 1 Answer
Mouse wheel zoom 1 Answer