Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 KyleJensen72 · Oct 10, 2015 at 12:48 AM · camerapositionlerpgunzoom

Changing camera position to shoot through scope on gun

Right now I have 2 Cameras: the main camera displays the gun at its normal state and a second camera is attached to the gun (the gun is a child of the main camera) and when toggled it looks through the scope of the gun and increases the field of view.

Heres a visual for a better understanding:

alt text

alt text

Now if I were to just toggle the second camera on and turn the main camera off, this would work splendid, but it's not very ideal. You should only have 1 camera per scene.

So I want to Lerp the position of the camera to look through the scope and manually decrease the fieldofview. So I have written the following script:

     public Camera ZoomedCamera;
 
     private Transform MainCameraTransform;
     private Transform ZoomedTransform;
 
     private bool zoomed = false;
 
     void Start () {
         MainCameraTransform = Camera.main.transform;
         ZoomedTransform = ZoomedCamera.transform;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.LeftShift)) 
         {
             zoomed = !zoomed;
         }
 
         if (zoomed) {
             MainCameraTransform.position = Vector3.Lerp (MainCameraTransform.position, ZoomedTransform.position, 5f * Time.deltaTime);
             MainCameraTransform.Rotate(ZoomedCamera.transform.rotation.eulerAngles);
         }
 
 
     }

The problem with this is that it doesn't work: when I hit the zoom button, the camera speeds through the scene at the speed of light and it's hard to tell exactly what is going on.

Could anyone give me some insight as to what Im doing wrong? I think it is something to do with the parent-child relationship, but even when I've tried using static values, I cannot seem to replicate the correct solution.

screen-shot-2015-10-09-at-120129-pm.jpg (294.4 kB)
screen-shot-2015-10-09-at-120155-pm.jpg (281.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Pharan · Oct 10, 2015 at 06:35 AM

(1) All the Lerp samples in the docs are misleading. Look up linear interpolation anywhere else on the internet. This is a slightly better description: https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.mathhelper.lerp.aspx?f=255&MSPPError=-2147217396

The third parameter of Lerp is not time. It's a value between 0 and 1 describing the weight of the second parameter.

0 means the resulting value will be equal to the value of your first argument, and 1 means it will be your second argument.

(2) transform.Rotate means it'll rotate it by a certain amount. And the way you set it up, it'll rotate infinitely, and really fast if the value it's getting is even moderate. If you want to set the rotation, use transform.rotation = yourNewRotation.

In this case, do read the docs. In fact, read all the stuff under Transform. It'll pay off in the long run. And read all of Vector3's docs too while you're at it.

(3) The other problem seems to be that there's no logic that returns your zoom level back to normal.

Comment
Add comment · Show 1 · 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 KyleJensen72 · Oct 10, 2015 at 06:38 PM 0
Share

Thanks, the reason I did rotate is because It wasnt letting me set the rotation, but Il try again :P Good advice, and I never set the zoom so that part isnt there yet ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Clamping a 2D camera while zooming. 0 Answers

Help with Lerp 0 Answers

Camera zoom smoothing 1 Answer

Simple camera zoom function 1 Answer

Wait before zooming camera at the start of level 1 Answer


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