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
2
Question by MrJames · Mar 29, 2010 at 03:04 PM · mouselook

Mouselook.js don't move smooth, its jittery

Hi, I rewrite the Mouselook.cs into my Mouselook.js. Also I figured out that everything worked fine with my solution:

public var sensitivityX : float = 15; public var sensitivityY : float = 15;

public var minimumX : float = -360; public var maximumX : float = 360;

public var minimumY : float = -60; public var maximumY : float = 60;

private var rotationX : float = 0; private var rotationY : float = 0;

private var xQuaternion : Quaternion; private var yQuaternion : Quaternion;

private var originalRotation : Quaternion;

function Start (){ if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; }

function Update(){ rotationX += Input.GetAxis("Mouse X") sensitivityX; rotationY += Input.GetAxis("Mouse Y") sensitivityY; if ((rotationX >= -360) && (rotationX <= 360)){ rotationX = Mathf.Clamp (rotationX, minimumX, maximumX); }else if (rotationX < -360){ rotationX = Mathf.Clamp (rotationX+360, minimumX, maximumX); }else{ rotationX = Mathf.Clamp (rotationX-360, minimumX, maximumX); }

 if ((rotationY &gt;= -360) &amp;&amp; (rotationY &lt;= 360)){
     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
 }else if (rotationY &lt; -360){ 
     rotationY = Mathf.Clamp (rotationY+360, minimumY, maximumY);
 }else{
     rotationY = Mathf.Clamp (rotationY-360, minimumY, maximumY);
 }
 xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
 yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

 transform.localRotation = originalRotation * xQuaternion * yQuaternion;

}

The script is connected to the MainCamera (FirsPersonController is parent > Graphics and Main Camera are children). But after a few tests i figured out that the camera don't move smooth. Everything seems to judder when I move the mouse.

-> I also recognized this with the standard MouseLook.cs. Any ideas what could evoke this?

Comment
Add comment · Show 1
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 MrJames · Apr 03, 2010 at 02:32 PM 0
Share

I just started my project all over again and use the "first person controller" prefab - so far everything works fine.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by qJake · Mar 29, 2010 at 06:02 PM

If the original MouseLook script was also "jittery" (the word you were probably looking for), then it's most likely a problem with your specific machine. I've had no problems in the past with using the MouseLook script and having it lag.

You should check to make sure that your computer isn't running anything in the background that may interfere with Unity, and also check to make sure your computer has enough resources to run Unity smoothly.

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 MrJames · Mar 29, 2010 at 06:29 PM 0
Share

$$anonymous$$aybe I expressed it wrong. Walking works without any problem. Even looking exactliy along the x-axis works fine. But any chance to the y-axis seems to jitter (thanks ;) Just the y-axis "calculation" seem to went wrong

avatar image
3
Wiki

Answer by Graham 1 · Nov 08, 2010 at 10:25 PM

Hi, I've been playing around trying to write a better mouse look function and came up with this. Delete the standard mouse look script from the first person controller parent object and also from the child camera. Attach the following script to the child camera only. It will control both the camera (for looking up and down) and also the parent object (for rotating left and right). It uses the Quaternion Lerp function to achieve smooth rotation and uses only a few lines of code.

private var xtargetRotation:float=10; private var ytargetRotation:float=10; var xSensitivity:float=10; var ySensitivity:float=10; var smoothing=2; var min=-60; var max=60;

function Update () {

var yAxisMove:float=Input.GetAxis("Mouse Y")*ySensitivity; // how much has the mouse moved? ytargetRotation+=-yAxisMove; // what is the target angle of rotation ytargetRotation=ytargetRotation % 360; ytargetRotation=Mathf.Clamp(ytargetRotation,min,max);

var xAxisMove:float=Input.GetAxis("Mouse X")*xSensitivity; // how much has the mouse moved? xtargetRotation+=xAxisMove; // what is the target angle of rotation xtargetRotation=xtargetRotation % 360;

transform.localRotation=Quaternion.Lerp(transform.localRotation,Quaternion.Euler(ytargetRotation,0,0),Time.deltaTime*10/smoothing); transform.parent.rotation=Quaternion.Lerp(transform.parent.rotation,Quaternion.Euler(0,xtargetRotation,0),Time.deltaTime*10/smoothing); }

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 Dom · Jan 13, 2011 at 06:26 AM 0
Share

great code!

this is an improvement on mouselook, camera moves so smoothly.

avatar image Orionark · Feb 15, 2012 at 11:37 PM 0
Share

This is a superb solution for a smooth FPS camera. I find that if you make the smoothing 0.5 or so, it makes the camera suitably accurate (not so much lag as a result of the smoothing), but it still feels much smoother than the default camera.

avatar image
1

Answer by pyro · May 20, 2010 at 11:25 AM

Put all of your actual camera movement code into LateUpdate() so that it's locked at being updated every 1 frame

function LateUpdate()
{
     transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}

Since yours is in the Update() it's being called many times per frame which is causing it to look jittery.

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 Mike 3 · May 20, 2010 at 11:31 AM 1
Share

Update is called once a frame only

avatar image Aubrey Hesselgren · Jan 27, 2011 at 01:04 PM 1
Share

It's still worth doing camera positioning in late update: it'll more or less assure that you're not positioning based on information which is updated later in the frame.

i.e. if camera.Update is called before cameraTarget.update, you're going to be looking at where cameraTarget WAS.

LateUpate assures that all the movement you've done is processed before positioning the camera.

avatar image
0

Answer by Ryan-Gatts · Feb 02, 2014 at 08:45 PM

For anyone who wants it, I translated this into C# on my way to modifying it for my own game. I hope this is helpful to somebody :) using UnityEngine; using System.Collections; using System.Collections.Generic;

 [AddComponentMenu("Camera-Control/Smooth Mouse Look")]
 public class SmoothMouseLook : MonoBehaviour 
 {
     public float xSensitivity = 10f;
     public float ySensitivity = 10f;
     
     public float smoothing = 1f;
     
     public float min = -60f;
     public float max = 60f;
     
     
     float xTargetRotation = 10f;
     float yTargetRotation = 10f;
     
     void Update ()
     {
         float yAxisMove = Input.GetAxis("Mouse Y")*ySensitivity; //how much has Mouse Y moved?
         yTargetRotation += -yAxisMove; //what is the new target rotation?
         yTargetRotation = yTargetRotation % 360; //get the remainder of targetRotation from 360
         yTargetRotation = Mathf.Clamp (yTargetRotation,min,max);
         
         float xAxisMove = Input.GetAxis ("Mouse X")*xSensitivity; //how much has Mouse X moved?
         xTargetRotation += xAxisMove; //what is the new target rotation?
         xTargetRotation = xTargetRotation % 360; //get the remainder of targetRotation from 360
         
         transform.localRotation     = Quaternion.Lerp (transform.localRotation, Quaternion.Euler(yTargetRotation,0,0), Time.deltaTime*10/smoothing);
         transform.parent.rotation     = Quaternion.Lerp (transform.parent.rotation, Quaternion.Euler(0,xTargetRotation,0), Time.deltaTime*10/smoothing);
     }
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Player moving in mouse direction and camera following player's rotation. 0 Answers

Mouse look script with no world up axis behaves weirdly 1 Answer

rotate player towards the mous 1 Answer

Toggle camera sensitivity 1 Answer

Disabling a child script within the parent. 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