Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ImpoliteSand868 · Mar 14, 2020 at 10:44 PM · camerascripting problemplayercamera-movementgameplay

How can I get a Capsule to move and rotate in the direction the cam is facing?

Hey, I cannot figure out how to make the player rotate in the same direction on the Rotation Y constraint from rigidbody. I have a script that unity has in their wiki, http://wiki.unity3d.com/index.php/SmoothMouseLook and I don't know how to implement it.

Example 1: most fps games have the ability to look through the players eyes, that is what I want. With the script referenced above, I can rotate the camera. But what I want is for the Player to move along the X, and Z, axis depending on where the eyes/Camera are looking. I created two scripts. One is called "CustomFPSCameraMove" (which uses the script referenced above" and the other is called "CustomFPSCapsuleMove" and this is the one I want to move the player('Capsule') along camera Y Rotation so I can move back, forwards, left, and right.

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 Link17x · Mar 14, 2020 at 11:36 PM

FPS games tend to hide the mouse cursor and rotate the camera (player perspective) towards whichever direction the mouse is moved. Then move your player using the usual horizontal and vertical inputs (typically WASD).


 public class MouseCursor : MonoBehaviour
     {
         private void Awake()
         {
             Cursor.lockState = CursorLockMode.Locked;
             Cursor.visible = false;
         }
 
         public void ToggleMouse(bool show)
         {
             Cursor.lockState = show
                 ? CursorLockMode.None
                 : CursorLockMode.Locked;
 
             Cursor.visible = show;
         }


Then control your camera rotation based on the mouse position. The camera might need to be a child object of an empty game object that acts as a pivot.

      [SerializeField] private Gameobject cameraPivot;
      [SerializeField] private float MouseSensitivity = 10;

         private void CameraControl()
         {
             // Make sure the mouse position is within our screen boundaries
             var screenRect = new Rect(0, 0, Screen.width, Screen.height);
 
             if (!screenRect.Contains(Input.mousePosition))
                 return;

             // Store a reference to the mouse X and Y position and control the sensitivity
             var vertical = Input.GetAxis("Mouse Y") * Time.deltaTime * MouseSensitivity;
             var horizontal = Input.GetAxis("Mouse X") * Time.deltaTime * MouseSensitivity;

             // Rotate the camera pivot on the X-axis only
             // Rotate the player on the Y-axis only
             cameraPivot.transform.Rotate(-vertical, 0, 0);
             transform.Rotate(0, horizontal, 0);

             // Set a limit of how far the camera can turn on the X and Y-axis 
             // to prevent 360 rotation
             var local = cameraPivot.transform.localEulerAngles;
 
             if (local.x <= 340 && local.x >= 90)
             {
                 cameraPivot.transform.localEulerAngles = new Vector3(340, 0, 0);
             }
             if (local.x >= -45 && local.x <= 120)
             {
                 cameraPivot.transform.localEulerAngles = new Vector3(0, 0, 0);
             }
         }


This script/method will need to be on the player. I have no idea what you will need/want the sensitivity to be set to, just play around with it in the inspector.

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

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

Multiple Targets Camera 1 Answer

How to do screenshake without influencing the transform of the camera? 2 Answers

Keep camera away from head while using Lerp 0 Answers

Error in roll a ball script: unkown where to address [ATM] 0 Answers

Moving Player Relative to Camera? 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