Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 shadow4004 · Apr 09, 2021 at 07:24 AM · cameracamera-movementcamera rotatecamera-lookcamera rotation

Camera gets stuck when cursor is locked

Hello there. This is my first time posting here and I'm hoping it'd be worth it. I'm trying to make a FPS character. I've got an empty gameObject as Player with a Capsule and a Camera as its children. This is the code I've written for the character to look around and assigned it to Player gameObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class MouseLook : MonoBehaviour {
public float mouseSensitivity = 50f;

 Transform cam;
 Vector2 mouseMovement;
 float mouseX;
 float mouseY;
 float xRotation = 0f;

 void Start()
 {
     cam = gameObject.transform.Find("Camera");
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
 }

 void Update()
 {
     mouseX = mouseMovement.x * mouseSensitivity * Time.deltaTime;
     mouseY = mouseMovement.y * mouseSensitivity * Time.deltaTime;

     xRotation -= mouseY;
     xRotation = Mathf.Clamp(xRotation, -90f, 90f);

     transform.Rotate(Vector3.up * mouseX);
     cam.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
 }

 void OnLook(InputValue mouseValue)
 {
     mouseMovement = mouseValue.Get<Vector2>();
 }

}

The problem started when I decided to lock the cursor. Before that it worked just fine expect the character could look around just as far as cursor could go. I couldn't rotate past when the cursor hit the edge of screen. So I decided to lock the cursor by using Cursor.lockState = CursorLockMode.Locked; After this tweak whenever I press the Play button the cursor doesn't get automatically locked, I have to click somewhere inside the Game view for the cursor to get locked. But when I do this and try to move the mouse the character starts looking towards any random position and gets stuck there. Mostly it's looking down. Moving the mouse or clicking doesn't work. Only when I press escape the cursor gets unlocked then I stop the game.
I'm using Unity 2020.3.3f1 on Ubuntu. I'm having this issue since 2020.3.1f1. Please let me know if there's anything wrong with the code or is there another way to solve the issue. All help is greatly appreciated.

Comment
Add comment · Show 2
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
avatar image Llama_w_2Ls · Apr 09, 2021 at 07:52 AM 0
Share

It seems to be a problem with entering the game view from the editor. If you build, is there still an issue?

avatar image shadow4004 Llama_w_2Ls · Apr 09, 2021 at 09:26 AM 0
Share

I just built and ran and it's working totally fine. Thank you for the suggestion. I can work with this as long as the built game is okay. Have a nice day/evening :)

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

181 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

Related Questions

How to allow camera to complete an upside down rotation while using LookAt() 0 Answers

Camera movement according to rotation and actual position RTS 1 Answer

camera zoom 1 Answer

Jerky 3rd Person Camera Following Movement and Rotation 0 Answers

camera movments fixed.. character controller without using character controller -.-' 2 Answers


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