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 Mayers · Oct 29, 2013 at 11:00 AM · camerarotationmousescreenfollow

rotate camera on mouse reaching edges

Hi, im working on a camera script where camera follows character and rotates when mouse is close to screen edges. I have 2 main issues, first, when coming from the "rotation" area to the "non rotation" area transform just snaps and i would like to keep rotation or the "angle" on character the second issue is that i would like the camera to just rotate left when the mouse is on the left side of the screen and not rotate back when coming back to center(same for right side) and increase rotation speed when getting closer to an edge.

 var ScreenWidthPercentage: float;
 var ScreenHeightPercentage: float;
 var rotationzone: boolean;
 
 var target : Transform;
 var distance = 10.0;
 var height:float=2.8;
 
 var xSpeed = 250.0;
 var ySpeed = 120.0;
 
 var yMinLimit = -15;
 var yMaxLimit = 80;
 
 private var x = 0.0;
 private var y = 0.0;
 private var newRotation: Quaternion;
 private var oldRotation: Quaternion;
 
 
 function Start () {
 
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // Make the rigid body not change rotation
        if (rigidbody)
         rigidbody.freezeRotation = true;
 }
 
 
 function Update(){
      // gives us 25% of the size of the screen (change the * 25 to choose the percentage you want)
 
     ScreenWidthPercentage = (Screen.width  * 25) / 100;
      ScreenHeightPercentage = (Screen.height  * 25) / 100;
     // when the cursor gets to less than the ScreenWidthPercentage from the left or right edge of the screen
 
     if(Input.mousePosition.x <= ScreenWidthPercentage || Input.mousePosition.x >= Screen.width - ScreenWidthPercentage || Input.mousePosition.y <= ScreenHeightPercentage || Input.mousePosition.y >= Screen.height - ScreenHeightPercentage){
         //turn on the SmoothFollow script
 
         rotationzone = true;
 
     }
 
     else {
 
         // turn off the follow script, because we're no longer at the edge of the screen
 
         rotationzone = false;
 
     }
 
 }
 
 function LateUpdate () {
 
     if (target) {
         if(rotationzone){
                if(Input.mousePosition.x <= ScreenWidthPercentage){//xSpeed related to mousepositionleft
             xSpeed = (ScreenWidthPercentage-Input.mousePosition.x)*10;
             }
             if(Input.mousePosition.x >= Screen.width - ScreenWidthPercentage){//xSpeed related to mousepositionright
             xSpeed = (Screen.width*0.75-Input.mousePosition.x)*-10;
             }
             if(Input.mousePosition.y <= ScreenHeightPercentage){//xSpeed related to mousepositiontop
             ySpeed = (ScreenHeightPercentage-Input.mousePosition.y)*1;
             }
             if(Input.mousePosition.y >= Screen.height - ScreenHeightPercentage){//xSpeed related to mousepositionbottom
             ySpeed = (Screen.height*0.75-Input.mousePosition.y)*-1;
             }
             
         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, height, -distance) + target.position;
         
         transform.rotation = Quaternion.Lerp (oldRotation, rotation, Time.deltaTime * 0.1f); 
         transform.position = position;
         
         }
         
         if(rotationzone==false)
        
             position = rotation * Vector3(0.0, height, -distance) + target.position;
                transform.rotation = rotation;
                transform.position = position;
    
     }
     oldRotation=transform.rotation;
 }
 
 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by segment · Feb 03, 2014 at 11:12 AM

Hi Mayers. I was facing the same issue that you and here is my code for you to try.

 protected void processMove(Vector3 delta)
     {
         //Moving crosshair
         crosshairPosition = new Rect(crosshairPosition.x + delta.x,
 crosshairPosition.y - delta.y, crosshairTexture.width, crosshairTexture.height);

     //make weapon 'look' at crosshair.
     var distance = (-weapon.transform.position.z * - mainCamera.transform.position.z) * 0.5f;
     var position = new Vector3(crosshairPosition.x + crosshairTexture.width/2, 
 Screen.height - crosshairPosition.y - crosshairTexture.height/2, distance);
     position = mainCamera.ScreenToWorldPoint(position);
     weapon.transform.LookAt(position);

     if (crosshairPosition.x <= 150f + crosshairTexture.width/2 &&
 delta.x < previousDeltaX)
     {
         rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
         rotationY += delta.x * Time.deltaTime * rotationSpeed;

         rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
         rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);

         previousDeltaX = delta.x;

         transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
     }
     else if (crosshairPosition.x >= Screen.width - 200f - crosshairTexture.width/2 
 && delta.x > previousDeltaX)
     {
         rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
         rotationY += delta.x * Time.deltaTime * rotationSpeed;

         rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
         rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);

         previousDeltaX = delta.x;

         transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
     }
     else if (crosshairPosition.y <= 100f - crosshairTexture.height/2 
 && delta.y > previousDeltaY)
     {
         rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
         rotationY += delta.x * Time.deltaTime * rotationSpeed;

         rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
         rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);

         previousDeltaY = delta.y;

         transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
     }
     else if (crosshairPosition.y >= Screen.height - 100f - crosshairTexture.height/2 
 && delta.y < previousDeltaY)
     {
         rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
         rotationY += delta.x * Time.deltaTime * rotationSpeed;

         rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
         rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);

         previousDeltaY = delta.y;

         transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
     }
     else
     {
         previousDeltaY = 0f;
         previousDeltaX = 0f;
     }
 }

delta is a diff between last cursor position in a last frame and current one in current frame.

Though I'm rotating my 'shooter' game object not the camera itself but you can pick up the idea (not so best but working though) of previous delta and delta.

Feel free to ask questions, Good luck!

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

16 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

Related Questions

Mouse.position from center of player 1 Answer

Need help on the 3ds max style camera control 0 Answers

How to draw a small circle on scene and moving it wherever mouse cursor moves 2 Answers

Camera following Pivot not working 1 Answer

Camera following/looking at aircraft 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