Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by CTatz · Feb 20, 2013 at 05:06 AM · camera2dsmoothclampshaking

Problems with 2D Camera Shaking

Hey guys! I've looked through a lot of the answers and managed to get some smooth camera code to follow my target. However I wanted to add bounds to the current level that the camera cannot see past, so I found the clamp function.

After adding the clamp function my camera worked correctly at around 60fps. Today I went to go test my game again and it was running at around 400fps (Not sure if this is relevant) but the camera was violently shaking and no changes to code have been performed.

Attached is my code and I am not sure what I need to change to fix this problem:

     void followPlayer(){
         
         float cameraClampX = Mathf.Clamp(transform.position.x, -15, 13);
         float cameraClampY = Mathf.Clamp(transform.position.y, 2, 14);
         
         Vector3 point = camera.WorldToViewportPoint(target.position);
         Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.4f, 0.4f, point.z)); //(new Vector3(0.5, 0.5, point.z));
         Vector3 destination = transform.position + delta;
         
         Vector3 currentplace = new Vector3(cameraClampX, cameraClampY, transform.position.z);
         //Vector3 cameraMove = Vector3.MoveTowards(currentplace, destination, 0.5f);
         Vector3 cameraMove = Vector3.SmoothDamp(currentplace, destination, ref velocity,  dampTime);
         transform.position = cameraMove;
 }

If anyone would like to explain to me a more efficient way of making this work or something that will even fix this problem I would be extremely grateful!

Thank you

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

3 Replies

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

Answer by CTatz · Feb 22, 2013 at 02:27 AM

Ah, for anyone looking the answer is pretty simple:

When having the camera follow a player make sure you have the clamp boundaries checking from the players position and not the camera! I guess it having to run through and check the cameras position from the past while the player is already somewhere else makes it seem all jerky.

Code that sort of explains it:

 float targetClampX = Mathf.Clamp(target.position.x, -15, 15);
             float targetClampY = Mathf.Clamp(target.position.y, 2, 14);
             Vector3 targetPosition = new Vector3(targetClampX, targetClampY, mainCameraZValue);
             transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
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 Nelis · Feb 20, 2013 at 02:18 PM

Are you sure that your camera is shaking or is it your player who is clipping

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 CTatz · Feb 20, 2013 at 04:20 PM 0
Share

If I take off the clamp then the camera runs smooth. Would that still happen if the player clipped?

avatar image
0

Answer by Perengano · Apr 22 at 02:30 AM

put the camera script in the last place of the script execution order

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

11 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

Related Questions

Unity smooth following 2D camera not working properly on different resolutions 0 Answers

Smooth Camera Follow 2 Answers

2d camera help! 0 Answers

Shaky player effect when I apply a damping camera follow script? 1 Answer

CAMERA SMOOTH 2D player is not on the left side 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