• 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 Darkolos · Jan 08, 2021 at 06:31 AM · collisioncolliderrandomcamera rotatesliding

FPS Character keeps randomly sliding and rotating

So im trying to make my first First Person Game I have been looking for a solution for a while now, but nothing seems to help. My character keeps randomly rotating and sliding the whole time and also buggs around while walking into walls. I spent 2 hours going through websites and nothing helped. The weird thing is the rotating part. A lot of people have the sliding but not a lot seem to have the random rotate.

I have also basically tried every way how to make collisions with any kind of kombinations of settings.

This is my entire code for my Character. Mind that a lot of the code is not related to the problem.. My Camera is simply a child of the Character, thats how i made it follow it. I know you could do A TON better with that code but trying to fix the problem discribed above is my highest priority. The last method in the code is my latest attempt to fix the problem myself. I would be super happy to read your solutions, even if you say I need to change my entire code.

Felix

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Threading;
 
 public class CharacterScript : MonoBehaviour
 {
     public GameObject Character;
     public Rigidbody CharacterRigidbody;
     public bool CurrentlyJumping = false;
     public bool CurrentlyLockingMouse = false;
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 1F;
     public float sensitivityY = 1F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -90F;
     public float maximumY = 90F;
 
     float rotationY = 0F;
     float rotationX = 0F;
 
     void Start()
     {
         LockAim();
 
     }
     void Update()
     {
      //   CharacterRigidbody.isKinematic = false;
         CheckMouseMovement();
         CheckWalking();
         CheckJump();
         CheckAim();
         CheckShoot();
         CheckEscape();
 
         if (GameObject.Find("Character").transform.position != GameObject.Find("Main Camera").transform.position)
         {
             GameObject.Find("Main Camera").transform.position = GameObject.Find("Character").transform.position;
         }
 
 
     }
     public void CheckWalking()
     {
         // Walking
         if (Input.GetKey(KeyCode.W))
         {
             transform.Translate(Vectors.WalkingVectorForward * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.S))
         {
 
             transform.Translate(Vectors.WalkingVectorBackwards * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.A))
         {
 
             transform.Translate(Vectors.WalkingVectorLeft * Time.deltaTime);
         }
         if (Input.GetKey(KeyCode.D))
         {
 
             transform.Translate(Vectors.WalkingVectorRight * Time.deltaTime);
         }
     }
     public void CheckJump()
     {
 
         if (Input.GetKeyDown("space"))
         {
             CurrentlyJumping = true;
         }
         if (CurrentlyJumping)
         {
             transform.Translate(Vectors.WalkingVectorJump, Space.World);
             Globals.JumpCounter = Globals.JumpCounter + 1;
             if (Globals.JumpCounter > Globals.JumpDuration)
             {
                 Globals.JumpCounter = 0;
                 CurrentlyJumping = false;
             }
         }
     }
     public void CheckAim()
     {
         //Aim
         if (Input.GetKeyDown("mouse 1"))
         {
             if (Globals.CurrentlyAiming)
             {
 
                 Globals.CurrentlyAiming = false;
             }
             else
             {
                 Globals.CurrentlyAiming = true;
             }
         }
     }
     public void CheckShoot()
     {
         if (Input.GetKeyDown("mouse 0"))
         {
 
         }
     }
     public void CheckEscape()
     {
         if (Input.GetKeyDown("escape"))
         {
             LockAim();
         }
 
     }
     public void LockAim()
     {
         if (CurrentlyLockingMouse)
         {
             Cursor.lockState = CursorLockMode.None;
             CurrentlyLockingMouse = false;
         }
         else
         {
             Cursor.lockState = CursorLockMode.Locked;
             CurrentlyLockingMouse = true;
         }
     }
     public void CheckMouseMovement()
     {
 
         if (axes == RotationAxes.MouseXAndY)
         {
 
             rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
 
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
 
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
 
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
         else
         {
 
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
 
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
 
     }
     public void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.name =="Room1")
         {
          //   CharacterRigidbody.isKinematic = true;
             CharacterRigidbody.velocity = Vector3.zero;
 
         }
     }
 
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by theBlueTornado · Jan 08, 2021 at 07:19 AM

try in unity lock rotation in the inspector where the Rigidbody.

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

Answer by Darkolos · Jan 08, 2021 at 05:41 PM

I dont know, i cant seem to find "lock rotation" anywhere in the inspector of the object the rigidbody is attached to. @theBlueTornado

Comment
Add comment · Show 1 · 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
avatar image Llama_w_2Ls · Jan 08, 2021 at 10:44 PM 0
Share

Look for the Constraints dropdown in the Rigidbody component in the inspector, and tick the boxes to lock rotation on the x and z axis. @Darkolos

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

200 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

Related Questions

Collision Detection is too slow 0 Answers

Prevent randomGenerated Structures from colliding / clipping 1 Answer

OnCollisionStay2D is ignoring OnCollisionEnter2D 2 Answers

Collision not working 1 Answer

Unwanted jittery behavior 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