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 hOswald · Oct 14, 2014 at 08:38 PM · camerazoomshoulder

Over the Shoulder Camera Not Working

Hello. I've been struggling on getting this over-the-shoulder camera to work. I'm trying to modify the MouseOrbit script so that the camera will zoom into an over-the-shoulder view when the right mouse button is clicked. Right now I have it set up so that the camera should zoom into a null Transform "starget" that is located near the shoulder of my character. The problem is that when the right mouse button is clicked this only happens for about a frame before everything snaps back into normal third person view. Also, when the right mouse button is down the camera should not be able to orbit anymore, but it is with what I have written.

Here is my code.

 var target : Transform;
 var distance = 10.0;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -20;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 private var baseFOV : float;
 
 /*Point that the camera should zoom in to when right 
 mouse button is clicked.*/
 public var starget : Transform;
  
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     baseFOV = Camera.main.fieldOfView;
 
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 function L() {
     /*Zoom camera to fixed position over shoulder when
     right mouse button is clicked*/
     if(Input.GetMouseButtonDown(1)){ 
         Camera.main.transform.LookAt(starget);
         Camera.main.fieldOfView = 20;
     }
     else{
         if(Input.GetMouseButtonUp(1)){
             Camera.main.transform.LookAt(target);
             Camera.main.fieldOfView = baseFOV;
         }
         /*Orbit camera around player character*/
         if (target){
             x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
                  
              y = ClampAngle(y, yMinLimit, yMaxLimit);
                         
             var rotation = Quaternion.Euler(y, x, 0);
             var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
                 
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 }
 
 static function ClampAngle (angle : float, min : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 }

Also, if there is a better way of accomplishing this, please let me know. I'm still getting used to coding and Unity and appreciate any help I can get.

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
0

Answer by sevensixtytwo · Oct 15, 2014 at 02:47 AM

I suggest you use a boolean variable to keep track of your zoom state.

 public var isZoomed : boolean;

This lets you easily stop that pesky mouseOrbit code from running when you're zoomed in.

 //Put this in your update
 if (isZoomed == false) {
 
      //MouseOrbit code
 
      }

Then you simply use a method to control when it zooms.

 function Zoom(action:boolean) {
 
      if (action == true) {
           Camera.main.transform.LookAt(starget);
           Camera.main.fieldOfView = 20;
           isZoomed = true;
           }
      else {
           Camera.main.transform.LookAt(target);
           Camera.main.fieldOfView = baseFOV;
           isZoomed = false;
           }
 
      }

And call it using Input.GetMouseButton.

 //Put these in your update
 if (Input.GetMouseButtonDown(1)) {
     Zoom(true);
     }
 
 if (Input.GetMouseButtonUp(1)) {
     Zoom(false);
     }

Other than that, I also suggest using MouseLook rather than MouseOrbit. You can use nested empty gameObjects with multiple MouseLooks to get that Over-The-Shoulder feel. But what you're doing will probably work too.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Zoom camera based on position between player an object 2 Answers

How can I zoom out a camera locked on the player? 0 Answers

Camera too zoomed in in built game 1 Answer

Save picture from camera with zoom 0 Answers

How to add Pinch to Zoom? 0 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