Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 usagi · Apr 03, 2012 at 06:14 PM · mousecamera-movementfirst person

Mouse Camera Control

In the project I'm working on I have a room and I want to allow the user to move in it in first person using just the mouse, like in this kind of project

On Unify Community I've found a good script called MouseCameraControl that allows me to almost reach my goal.

 using UnityEngine;
 
 [AddComponentMenu("Camera-Control/Mouse")]
 public class MouseCameraControl : MonoBehaviour
 {
     // Mouse buttons in the same order as Unity
     public enum MouseButton { Left = 0, Right = 1, Middle = 2, None = 3 }
 
     [System.Serializable]
     // Handles left modifiers keys (Alt, Ctrl, Shift)
     public class Modifiers
     {
         public bool leftAlt;
         public bool leftControl;
         public bool leftShift;
 
         public bool checkModifiers()
         {
             return (!leftAlt ^ Input.GetKey(KeyCode.LeftAlt)) &&
                 (!leftControl ^ Input.GetKey(KeyCode.LeftControl)) &&
                 (!leftShift ^ Input.GetKey(KeyCode.LeftShift));
         }
     }
 
     [System.Serializable]
     // Handles common parameters for translations and rotations
     public class MouseControlConfiguration
     {
 
         public bool activate;
         public MouseButton mouseButton;
         public Modifiers modifiers;
         public float sensitivity;
 
         public bool isActivated()
         {
             return activate && Input.GetMouseButton((int)mouseButton) && modifiers.checkModifiers();
         }
     }
 
     [System.Serializable]
     // Handles scroll parameters
     public class MouseScrollConfiguration
     {
 
         public bool activate;
         public Modifiers modifiers;
         public float sensitivity;
 
         public bool isActivated()
         {
             return activate && modifiers.checkModifiers();
         }
     }
 
     // Yaw default configuration
     public MouseControlConfiguration yaw = new MouseControlConfiguration { mouseButton = MouseButton.Right, sensitivity = 10F };
 
     // Pitch default configuration
     public MouseControlConfiguration pitch = new MouseControlConfiguration { mouseButton = MouseButton.Right, modifiers = new Modifiers{ leftControl = true }, sensitivity = 10F };
 
     // Roll default configuration
     public MouseControlConfiguration roll = new MouseControlConfiguration();
 
     // Vertical translation default configuration
     public MouseControlConfiguration verticalTranslation = new MouseControlConfiguration { mouseButton = MouseButton.Middle, sensitivity = 2F };
 
     // Horizontal translation default configuration
     public MouseControlConfiguration horizontalTranslation = new MouseControlConfiguration { mouseButton = MouseButton.Middle, sensitivity = 2F };
 
     // Depth (forward/backward) translation default configuration
     public MouseControlConfiguration depthTranslation = new MouseControlConfiguration { mouseButton = MouseButton.Left, sensitivity = 2F };
 
     // Scroll default configuration
     public MouseScrollConfiguration scroll = new MouseScrollConfiguration { sensitivity = 2F };
 
     // Default unity names for mouse axes
     public string mouseHorizontalAxisName = "Mouse X";
     public string mouseVerticalAxisName = "Mouse Y";
     public string scrollAxisName = "Mouse ScrollWheel";
 
     void LateUpdate ()
     {
         if (yaw.isActivated())
         {
             float rotationX = Input.GetAxis(mouseHorizontalAxisName) * yaw.sensitivity;
             transform.Rotate(0, rotationX, 0);
         }
         if (pitch.isActivated())
         {
             float rotationY = Input.GetAxis(mouseVerticalAxisName) * pitch.sensitivity;
             transform.Rotate(-rotationY, 0, 0);
         }
         if (roll.isActivated())
         {
             float rotationZ = Input.GetAxis(mouseHorizontalAxisName) * roll.sensitivity;
             transform.Rotate(0, 0, rotationZ);
         }
 
         if (verticalTranslation.isActivated())
         {
             float translateY = Input.GetAxis(mouseVerticalAxisName) * verticalTranslation.sensitivity;
             transform.Translate(0, translateY, 0);
         }
 
         if (horizontalTranslation.isActivated())
         {
             float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
             transform.Translate(translateX, 0, 0);
         }
 
         if (depthTranslation.isActivated())
         {
             float translateZ = Input.GetAxis(mouseVerticalAxisName) * depthTranslation.sensitivity;
             transform.Translate(0, 0, translateZ);
         }
 
         if (scroll.isActivated())
         {
             float translateZ = Input.GetAxis(scrollAxisName) * scroll.sensitivity;
 
             transform.Translate(0, 0, translateZ);
         }
     }
 
 }





The last problem I'm not able to solve is about the forward and backward movement because with that script, after the activation of the "Depth Transition" functionality, the user has to continuously move the mouse to move the camera.

Is there a way to modify this script to have a navigation like the one in the example?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Roll a ball camera with rotating by a mouse? 1 Answer

Problem With First Person View Controller 1 Answer

Use mouse to move camera along fixed axis through city scene 0 Answers

Lock the Mouse/ Camera-Movement 1 Answer

Unity 5 Center Cursor 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