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 /
  • Help Room /
avatar image
0
Question by johnpauwels74 · Jun 22, 2020 at 09:11 PM · camerarotatecamera-movementcamera rotatepan

Problem with pan and rotate camera with mouse

I created a script that rotates and pans the camera using the mouse. It's like an RTS style pan where if you move your mouse toward the edge of the screen, it'll pan the camera along the x and z axes. I also set it so that if you hold the right mouse button and drag, it'll rotate the camera.

However, the problem I'm having is that if I rotate the camera and then try to pan, then the panning is going the wrong direction. Since it's using the edge of the screen near the mouse pointer to detect where to move along the screen's x and y axes, then it'll always pan the screen up along the y axis when I put my pointer at the top of the screen. This doesn't work because I may have rotated the screen and y is no longer up.

Here's the code I'm currently using. Does anyone know how to make an adjustment to the code that detects my pointer location so that if I move the mouse to the sides of the screen, it'll always pan in the correct direction like most RTS games do?

using System; using UnityEngine;

public class CameraController : MonoBehaviour { public Vector3 newPosition;

 public float panSpeed = 20f;
 public float panBorderThickness = 10f;
 public Vector2 panLimit;

 public float scrollSpeed = 10f;
 public float minY = 20f;
 public float maxY = 120f;

 private float mouseX;

 public Vector3 rotateStartPosition;
 public Vector3 rotateCurrentPosition;
 private Quaternion newRotation;

 void LateUpdate()
 {

     HandleMouseRotation();

     Vector3 pos = transform.position;

     if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
     {
         pos.z += panSpeed * Time.deltaTime;
     }
     if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
     {
         pos.z -= panSpeed * Time.deltaTime;
     }
     if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)
     {
         pos.x += panSpeed * Time.deltaTime;
     }
     if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
     {
         pos.x -= panSpeed * Time.deltaTime;
     }
     float scroll = Input.GetAxis("Mouse ScrollWheel");
     pos.y -= scroll * scrollSpeed * 100f * Time.deltaTime;

     pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x);
     pos.y = Mathf.Clamp(pos.y, minY, maxY);
     pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);

     transform.position = pos;

     mouseX = Input.mousePosition.x;
 }

 public void HandleMouseRotation()
 {
     var easeFactor = 10f;


     if (Input.GetMouseButton(1))
     {
         Debug.Log("mousex stored: " + Input.mousePosition.x);
         Debug.Log("mouseX: " + mouseX);
         if (Input.mousePosition.x != mouseX)
         {
             var cameraRotationY = (Input.mousePosition.x - mouseX) * easeFactor * Time.deltaTime;
             this.transform.Rotate(0, cameraRotationY, 0);
             Debug.Log("rotationlog");
         }

         rotateStartPosition = Input.mousePosition;
         Vector3 difference = rotateStartPosition - rotateCurrentPosition;

         rotateStartPosition = rotateCurrentPosition;
         newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
     }
 }

}

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.

Follow this Question

Answers Answers and Comments

277 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Locking Camera's rotation on a rolling character 0 Answers

Relative movement isn't changing based on camera orientation. 0 Answers

I cannot move my camera in the Game mode 0 Answers

Camera controls not working quite right 0 Answers

Make Camera follow rotation of 2 Players 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