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 /
avatar image
1
Question by ZeyFight · Jul 02, 2021 at 05:03 PM · scripting problemrigidbodycollider3dfps

My player's camera displays jerky objects

Hello ! I am in an unity fps project so I create a script to move my player and to move the camera but the problem is when I move my player and the camera at the same time looking at an object. The object is jerky ... (https://youtu.be/qQ1G3LH0aS4)


PlayerController :


 using UnityEngine;
  
 [RequireComponent(typeof(Rigidbody))]
 public class PlayerController : MonoBehaviour
 {
     private Rigidbody rb;
     public LayerMask layerMask;
  
     public bool Grounded;
  
     public float speed = 6;
     public float speedBoost = 5;
     public float JumpForce = 4;
  
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
  
     void FixedUpdate()
     {
         //Input
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
  
         //Moving
         Vector3 direction = new Vector3(x, 0, z);
         rb.MovePosition(rb.position + transform.TransformDirection(direction) * speed * Time.fixedDeltaTime);
         if (direction.sqrMagnitude > 1)
             direction.Normalize();
         //transform.Translate(direction * speed * Time.fixedDeltaTime);
         //rb.AddForce(direction * speed * Time.fixedDeltaTime);
  
         //Grounding
         Grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
  
         //Jumping
         if (Input.GetKeyDown(KeyCode.Space) && Grounded)
         {
             rb.velocity = new Vector3(rb.velocity.x, JumpForce, rb.velocity.z);
         }
  
         //Sprinting
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             speed += speedBoost;
         }
         else if (Input.GetKeyUp(KeyCode.LeftShift))
         {
             speed -= speedBoost;
         }
     }
 }



PlayerLook :


 using UnityEngine;
  
 public class PlayerLook : MonoBehaviour
 {
     public Transform player;
  
     public float mouseSensitivity = 4;
     private float x = 0;
     private float y = 0;
  
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
  
     void Update()
     {
         //Input
         x += -Input.GetAxis("Mouse Y") * mouseSensitivity;
         y += Input.GetAxis("Mouse X") * mouseSensitivity;
  
         //Clamping
         x = Mathf.Clamp(x, -90, 90);
  
         //Rotation
         transform.localRotation = Quaternion.Euler(x, 0, 0);
         player.transform.localRotation = Quaternion.Euler(0, y, 0);
     }
 }
 






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
0

Answer by Nistroy · Jul 04, 2021 at 09:33 AM

Try this code @ZeyFight , I just changed to MoveTowards to make the rotation smoother.

 public Transform player;
     public float lookSpeed = 10f;
 
     public float mouseSensitivity = 4;
     private float x = 0;
     private float y = 0;
 
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     void Update()
     {
         //Input
         x += -Input.GetAxis("Mouse Y") * mouseSensitivity;
         y += Input.GetAxis("Mouse X") * mouseSensitivity;
 
         //Clamping
         x = Mathf.Clamp(x, -90, 90);
 
         //Rotation
         transform.localRotation = Quaternion.Euler(Vector3.MoveTowards(transform.localRotation.eulerAngles, new Vector3(x, 0, 0), lookSpeed * Time.deltaTime));
          player.transform.localRotation = Quaternion.Euler(0, y, 0);
     }
Comment
Add comment · Show 2 · 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 ZeyFight · Jul 04, 2021 at 12:28 PM 0
Share

Hello ! Thank you for your reply ! I tried your script but the problem is still present ... Your script changed my vertical axis, I had trouble moving up and down and when I moved up, the camera kept going up without me even touch my mouse

avatar image Nistroy ZeyFight · Jul 06, 2021 at 03:46 PM 0
Share

Afterwards I don't know if you know it, but when you play your game on unity it's normal that your game works less well than when you build it. It is to improve performance that it sometimes gets weird. Sorry if my code doesn't work completely, I think the other user's response should work better!

avatar image
0

Answer by andrew-lukasik · Jul 04, 2021 at 12:30 PM

Makes mouse look as smooth as silk.


note: this code won't help with ugly jitter coming from controller movement. It is there 100% because a camera being parented to a Rigidbody is sure way to produce it. And the lower the framerate the more visible it becomes.


 using UnityEngine;
 
 public class PlayerLook : MonoBehaviour
 {
     [SerializeField] Transform _player;
     [SerializeField] float _mouseSensitivity = 100f;
     [SerializeField][Min(1f/50f)] float _mouseInterpolationTime = 0.1f;
     ( float horizontal , float vertical ) _mouse, _mouseDir, _mouseInterpolated, _angle;
 
     void Start ()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     void Update ()
     {
         _mouse = ( horizontal: Input.GetAxisRaw("Mouse X") , vertical: Input.GetAxisRaw("Mouse Y") );
         
         if( _mouse.horizontal<0 ) _mouseDir.horizontal = -1;
         if( _mouse.horizontal>0 ) _mouseDir.horizontal = 1;
         if( _mouse.vertical<0 ) _mouseDir.vertical = -1;
         if( _mouse.vertical>0 ) _mouseDir.vertical = 1;
         // note: don't replace this with Mathf.Sign as 0 must stay ignored
     }
 
     void FixedUpdate ()
     {
         float deltaTime = Time.fixedDeltaTime;
 
         // look left-right
         if( Mathf.Sign(_mouseInterpolated.horizontal)==_mouseDir.horizontal )
         {
             _mouseInterpolated.horizontal = Mathf.Lerp(
                 _mouseInterpolated.horizontal ,
                 _mouse.horizontal * _mouseSensitivity ,
                 (1f/_mouseInterpolationTime) * deltaTime
             );
         }
         else
         {
             _mouseInterpolated.horizontal = _mouse.horizontal;
         }
         _angle.horizontal += _mouseInterpolated.horizontal * deltaTime;
         _player.localRotation = Quaternion.Euler( 0 , _angle.horizontal , 0 );
 
         // look up-down
         if( Mathf.Sign(_mouseInterpolated.vertical)==_mouseDir.vertical )
         {
             _mouseInterpolated.vertical = Mathf.Lerp(
                 _mouseInterpolated.vertical ,
                 _mouse.vertical * _mouseSensitivity ,
                 (1f/_mouseInterpolationTime) * deltaTime
             );
         }
         else
         {
             _mouseInterpolated.vertical = _mouse.vertical;
         }
         _angle.vertical = Mathf.Clamp( _angle.vertical - _mouseInterpolated.vertical * deltaTime , -90f , 90f );
         transform.localRotation = Quaternion.Euler( _angle.vertical , 0 , 0 );
     }
 
 }
Comment
Add comment · Show 2 · 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 ZeyFight · Jul 04, 2021 at 04:45 PM 0
Share

Hello ! Thank you for your reply ! it seems to work but I feel a force when I move my mouse ... I try to modify the sensitivity, it does not change anything ... What to do?

avatar image andrew-lukasik ZeyFight · Jul 04, 2021 at 06:26 PM 0
Share

I updated the code. Change in mouse look direction overwrites interpolation.

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

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

Best way for rigidbody enemy? 1 Answer

FPS recoil 1 Answer

Problem with multiplayer unity. 1 Answer

Low framerate on physics initialization. 0 Answers

Use MoveRotation to Look At Another Object 0 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