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 /
This question was closed Nov 01, 2014 at 03:48 AM by Kirbyrawr for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Kirbyrawr · Oct 10, 2014 at 02:10 AM · camerarotationflickeringmathf.clamp

Mathf.Clamp issues

Hi there, so i have a script for camera movement/control and it was giving me issues related to a clamp it was going beyond the boundaries and returning to the position instead of just clamp, so i decided to do a new script more simple, and it's doing the same.

Previous problem: The other script which is more advanced and better done have those problems too.

Problem: If you use this script in a camera and move fast the mouse to down or up it will go beyond the clamp and then return to the clamp.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
 public class MouseLook2 : MonoBehaviour{
 
     Vector2 mouseDelta;
     Vector3 absolutePosition;
     Vector3 finalRotation;
     Vector3 smoothedPosition;
 
 
     public Vector2 yLimit = new Vector2(-75,150);
     public Vector2 sensitivity = new Vector2(2, 2);
     public GameObject characterBody;
 
     public void LateUpdate(){
 
     mouseDelta = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
         
 
         //Add inverted mouse delta value of Y (If we want inverted view just delete the negative sign)
         absolutePosition.x = -mouseDelta.y;
         //Add Mouse delta value of X
         absolutePosition.y = mouseDelta.x;
 
         finalRotation = absolutePosition;
         finalRotation = new Vector3(Mathf.Clamp(finalRotation.x,-yLimit.y,-yLimit.x),0,0);
         transform.localEulerAngles += finalRotation;
 
     }
 
 
 }

Thanks in advanced.

Comment
Add comment · Show 4
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 KayelGee · Oct 10, 2014 at 06:31 AM 0
Share

When dealing with rotations I would suggest you read up on how to use Quaternions. Also for smoothing look up Lerp and Slerp.

avatar image NoseKills · Oct 10, 2014 at 07:13 AM 0
Share

What @awest said is at least one of the problems.

You're limiting how much the camera rotation can change in one LateUpdate() but not it's max rotations.

avatar image Kirbyrawr · Oct 10, 2014 at 02:52 PM 0
Share

@awest ah i will try to see what's wrong.

@$$anonymous$$ayelGee yeah of course this script was only a test the bigger one have lerps and quaternions code.

@Nose$$anonymous$$ills mmm i see i will try to see what can i do.

Thanks a lot for the replies!

avatar image Kirbyrawr · Oct 10, 2014 at 03:28 PM 0
Share

@awest and @Nose$$anonymous$$ills

transform.localEulerAngles += finalRotation; transform.localEulerAngles = new Vector3($$anonymous$$athf.Clamp(transform.localEulerAngles.x,-yLimit.y,-yLimit.x),0,0);

Got it working, thanks a lot, i think both of you should put the comment as answer so i can accept it, thanks a lot ^^!

@$$anonymous$$ayelGee thanks a lot to you too sir ^^!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by awest · Oct 10, 2014 at 03:46 AM

Thanks @NoseKills @KayelGee. I had to go to work. Glad you were able to see it through.

The problem was this line because it added a clamped value to an unrestricted variable.

 transform.localEulerAngles += finalRotation;

It was fixed by being changed to the following line.

 transform.localEulerAngles = new Vector3(Mathf.Clamp(transform.localEulerAngles.x,-yLimit.y,-yLimit.x),0,0);

I've never done one of these past-tense. It feels weird.

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

Follow this Question

Answers Answers and Comments

31 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

Related Questions

Limit Rotation of Camera around a player using keyboard 1 Answer

This is probably a really simple problem that I can't figure out 1 Answer

How do I properly rotate an FPS camera with a joystick? 1 Answer

Local Third Person Movement based off Camera Rotation 0 Answers

Rotate according to the camera on Y and Z only! 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