Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kag359six · Aug 17, 2012 at 06:37 PM · camerazoommousewheel

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));
 
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 17, 2012 at 07:50 PM 0
Share

$$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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kag359six · Aug 20, 2012 at 05:06 PM 0
Share

How can I do this smoothly? It works but its very jittery since its exact...

avatar image Khada · Aug 20, 2012 at 05:12 PM 0
Share

$$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 :)

avatar image Khada · Aug 20, 2012 at 06:32 PM 0
Share

Did that work? Don't forget to mark the question as answered.

avatar image Bunny83 · Aug 20, 2012 at 10:16 PM 0
Share

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.

avatar image kag359six · Aug 21, 2012 at 04:11 AM 0
Share

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.

Show more comments
avatar image
1

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);
 
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khada · Aug 22, 2012 at 09:23 AM 0
Share

Nice. Did you manage to smooth the camera movement or is this your work around? Just curious :)

avatar image Bunny83 · Aug 22, 2012 at 04:01 PM 0
Share

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.

avatar image kag359six · Aug 22, 2012 at 04:27 PM 0
Share

yeah it does thanks. First time using the scroll wheel for anything i didnt know you could use it like that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Scoping script /Camera zoom preferably C# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges