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 /
  • Help Room /
avatar image
0
Question by Sergiyha · Jun 18, 2016 at 01:15 AM · camerartsmathfclamp

RTS Camera wiggle approaching to clamped borders

I made borders for my RTS Camera using Mathf.Clamp but it started wiggling when the borders reached. I need smooth movements but Clamp doesnt work fine and my camera always cross the borders a little bit with floating position value. Any ideas ? This is my Code but I think , you guys needn't it: Clamp : transform.position = new Vector3( Mathf.Clamp(transform.position.x, cameraLimits.leftLimit, cameraLimits.rightLimit), transform.position.y, Mathf.Clamp(transform.position.z, cameraLimits.downLimit, cameraLimits.upLimit));

Translation Camera:

public Vector3 GetTranslation() { float moveX = 0.0f; float moveZ = 0.0f; float moveY = 0.0f;

     float moveSpeed = cameraMoveSpeed * Time.deltaTime;


     if (Input.mousePosition.x < mouseScrollLimits.leftLimit)
     {
        // Debug.Log("Left");
         moveX = -moveSpeed;
     }
     else if (Input.mousePosition.x > Screen.width - mouseScrollLimits.rightLimit)
     {
        // Debug.Log("Right");
         moveX = moveSpeed;
     }
     else if (Input.mousePosition.y > Screen.height - mouseScrollLimits.upLimit)
     {
        // Debug.Log("up");

         moveZ = moveSpeed * Mathf.Cos(transform.rotation.eulerAngles.x * (Mathf.PI / 180));
         moveY = moveSpeed * Mathf.Sin(transform.rotation.eulerAngles.x * (Mathf.PI / 180));

     }
     else if (Input.mousePosition.y < mouseScrollLimits.downLimit)
     {
        // Debug.Log("down");
         moveZ =- moveSpeed * Mathf.Cos(transform.rotation.eulerAngles.x * (Mathf.PI / 180));
         moveY =- moveSpeed * Mathf.Sin(transform.rotation.eulerAngles.x * (Mathf.PI / 180));

     }
     return new Vector3(moveX, moveY, moveZ);
 }
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

1 Reply

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

Answer by JedBeryll · Jun 18, 2016 at 05:29 AM

 Mathf.Clamp(transform.position.x ...

This implies you assign the position then reassign it with the clamped value. If that's the case, the solution is to not assign it twice.

 Vector3 pos = GetTranslation();
 transform.position = new Vector3( Mathf.Clamp(pos.x + transform.position.x, cameraLimits.leftLimit, cameraLimits.rightLimit), pos.y + transform.position.y, Mathf.Clamp(pos.z + transform.position.z, cameraLimits.downLimit, cameraLimits.upLimit));
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 Sergiyha · Jun 18, 2016 at 12:36 PM 0
Share

Thx JedBeryll, nice idea about "not assign it twice". I found the issue. Translation was after clamping in the same frame. So I replace translation upper;

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

90 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

Related Questions

how do i Limit camera rotation on Y-Axis 1 Answer

Unity getting wrong transform.position from level image when trying to clamp camera within level image 0 Answers

Clamp camera within bounds of a Polygon Collider? 1 Answer

Clamping a value between two radiuses 1 Answer

Clamping a camera with weird results 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