• 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 Jakeostar · May 28, 2014 at 11:50 AM · followingmaincamera

Camera Is constanly jumping between two points

I have this problem with my script it was working fine until i brought it home to continue working on it. the problem is when i hold either the left or right mouse button the camera will look as if it is trying to move to two different points i made it so that in the inspector it is set to None and it yet the problem still occur. could this be a problem with the script. it is below if you can see an errors please notify me, i really need to sort this our. Also it is meant to be like an MMO camera where it will snap back to a certain position but you, look around your character with left mouse but turn with the right mouse.

 var target : Transform;
 
 var distance = 10.0;
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 var yMinLimit = -20;
 var yMaxLimit = 80;
 var distanceMin = 3;
 var distanceMax = 15;
 
 private var x = 0.0;
 private var y = 0.0;
 
 var cameraPos : GameObject;
 var camActPos : GameObject;
 var Plyr : GameObject;
 
 var horizontalSpeed : float = 3.0;
 var verticalSpeed : float = 2.0;
 private var count : int = 0;
 
 private var isleftmouse : boolean;
 private var isrightmosue : boolean;
 
 var standardPos : Transform;            // the usual position for the camera, specified by a transform in the game
 var lookAtPos : Transform;        
 var smooth : float = 3f;    // the position to move the camera to when using head look
 
 
 @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
 function Start () 
 {
 
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     isleftmouse = false;
     isrightmosue = false;
     // Make the rigid body not change rotation
     //if (rigidbody)
         //rigidbody.freezeRotation = false;
 
     // initialising references
     /*standardPos = GameObject.Find ("CamPos").transform;
     
     if(GameObject.Find ("LookAtPos"))
     lookAtPos = GameObject.Find ("LookAtPos").transform;
 */
     
 
 }
 
  
 
 function Update () 
 {
     /*
     if(Input.GetButton("Fire2") && lookAtPos)
         {
             // lerp the camera position to the look at position, and lerp its forward direction to match 
             transform.position = Vector3.Lerp(transform.position, lookAtPos.position, Time.deltaTime * smooth);
             transform.forward = Vector3.Lerp(transform.forward, lookAtPos.forward, Time.deltaTime * smooth);
         }
         else
         {    
             // return the camera to standard position and direction
             transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.deltaTime * smooth);    
             transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.deltaTime * smooth);
         }*/
 
 
     if(Input.GetKey("mouse 1"))
     {
         isrightmouse = true;
         print("KeyPressed1");
 
         if(isrightmouse == true)
         {
             if (target) 
             {
                 if(Input.GetMouseButton(1))
                 {
 
                     Screen.showCursor = false;
                     x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
                     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
                     var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
                     //var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
                     Plyr.transform.Rotate (0, h, 0);
 
                 }
                 else
                 {
                     //print("hello");
                     
                 }
             }
 
                    y = ClampAngle(y, yMinLimit, yMaxLimit);
                 var rotation = Quaternion.Euler(y, x, 0);
                 distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
                 var hit : RaycastHit;
 
                 if (Physics.Linecast (target.position, transform.position, hit)) 
                 {
                     distance -=  hit.distance;
                 }
                       var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
                     transform.rotation = rotation;
                     transform.position = position;
         }
     }
     else
     {
         isrightmouse = false;
     }
 
     if(Input.GetKey("mouse 0"))
     {
         isleftmouse = true;
         print("KeyPressed0");
 
         if(isleftmouse == true)
         {
              if (target) 
             
             {
                 if(Input.GetMouseButton(0))
                 {
 
                     Screen.showCursor = false;
                     x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
                     y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
                   
                 }
                 else
                 {
                     gameObject.transform.position = new Vector3(5.7,3,0.5);
 
                 }
             }
             
 
                    y = ClampAngle(y, yMinLimit, yMaxLimit);
                 var rotation2 = Quaternion.Euler(y, x, 0);
                 distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
                 var hit2 : RaycastHit;
 
                 if (Physics.Linecast (target.position, transform.position, hit)) 
                 {
                     distance -=  hit.distance;
                 }
                       var position2 = rotation2 * Vector3(0.0, 0.0, -distance) + target.position;
                     transform.rotation = rotation2;
                     transform.position = position2;
         }
     }
 
        else
        {
            isleftmouse = false;
        }
 }
 
  
 
  
 
 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);
 
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I zoom out my Field of View slowly? 1 Answer

How to import the object from server to unity 2 Answers

Material doesn't have a color property '_Color' 4 Answers

How can you make an enemy follow you only when look away or blink? 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