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 /
  • Help Room /
avatar image
0
Question by julianbesso1 · Nov 29, 2020 at 02:06 PM · camerafollowsmoothdamp

Camera Follow with SmoothDamp one axis jitter

Hello everyone! This is my first time building a game from scratch and I have a few questions.

The game I'm working on is a rail shooter and the issue is related to the CameraFollow script.

My problem: I have a camera that follows the player, but without leaving a predefined area (5x5). The player position is also clamped by using 'Camera.WorldToViewportPoint' and 'Camera.ViewportToWorldPoint'.

I use SmoothDamp, but for some reason when the player moves in the Y-axis the Camera follows it with jitter.

Code:

 public class CameraFollow : MonoBehaviour
 {
        public Vector3 velocity= Vector3.zero;
        // some other variables
             
        void Update()
        {
           FollowTarget(target);
        }
 
        void LateUpdate()
        {
           Vector3 localPos = transform.localPosition;
           transform.localPosition = new Vector3(Mathf.Clamp(localPos.x, -limits.x, limits.x), Mathf.Clamp(localPos.y, -limits.y, limits.y), localPos.z);
        }    
 
        public void FollowTarget(Transform t)
        {
           Vector3 localPos = transform.localPosition;
           Vector3 targetLocalPos = t.transform.position;
           transform.localPosition = Vector3.SmoothDamp(localPos, new Vector3(targetLocalPos.x + offset.x, targetLocalPos.y + offset.y, targetLocalPos.z + offset.z), ref velocity, smoothTime);    
        }
 
 }


I tried changing the velocity and even placing the FollowTarget function inside LateUpdate and FixedUpdate (which I'm still learning) but with no success.

My questions:
1 - What could be the reason behind that movement with jitter only in Y-axis? Could it be related to the screen size (mine is 1920x1080) ?
2 - Another thing that I noticed is that when I move the player to the upper-right corner, velocity value in X is greater that Y. Why is that?
3 - As regards smoothTime, I cannot pick a value greater that 0.125 without worsen the situation (for both axis). Am I missing anything else? maybe related to the 2 last parameters in SmoothDamp

Thanks in advance!

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 highpockets · Nov 29, 2020 at 08:55 PM

I don’t think you should use Update() for smooth damp. It is typically used in LateUpdate() after all other calculations are computed. Right now you are moving the camera in Update and then doing your clamping before rendering in LateUpdate. Also, if the player is already clamped, is it necessary to have the Camera clamped??


Instead of having an offset vector, you may want to look at just having an empty game object as a child of your player that is placed at the offset position then just use that empty as the camera target for the smooth damp.If you need to adjust the empty target position to clamp it in some way, just move that target and the camera will follow via the smooth damp which I’m sure will help with jitter.


Also, you are not calculating the camera smooth damp properly. You are passing in the target transforms world position as the target smooth damp position, but you are trying to update the local position of the camera. Since you are passing the world position of the target, you should work with the world position of the camera as well.


 [SerializeField] Transform target; //Empty transform placed as child on player at offset
 
 Vector3 velocity = Vector3.zero;
 [SerializeField] Vector2 limits;
 
 float smoothTime = 0.4f;
 
 void LateUpdate()
 {
     if(Mathf.Abs(target.position.x) > limits.x || Mathf.Abs(target.position.y) > limits.y)
     {
         target.position = new Vector2( Mathf.Clamp(target.position.x, -limits.x, limits.x), Mathf.Clamp(target.position.y, -limits.y, limits.y);
     }
     transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
 }



That should work for your situation

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 julianbesso1 · Nov 30, 2020 at 10:48 PM

Thanks for answering. This is what I'm trying to achieve:

alt text

As you can see, the camera cannot leave the square, but the player has a wider area of movement. That's why I'm clamping player's position with 'Camera.WorldToViewportPoint' and 'Camera.ViewportToWorldPoint' (inside Update()).
I also tried moving 'FollowTarget' method of 'CameraFollow' script to LateUpdate() but it made everything worse (jitter all the time)
Please forget about the offset. In my case, I only modified the Z-axis because I want the camera to be 5 units behind the player.


untitledx.png (137.2 kB)
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 julianbesso1 · Dec 01, 2020 at 10:17 AM 0
Share

For some reason now it's working fine on both axis. The only thing pending is solving the jitter when the Player reaches the boundaries..

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

288 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

Related Questions

Mathf.Clamp does not work correctly in trannsform.position I'm trying to create a 2.5D game but I'm having trouble limiting camera movement 0 Answers

Scripting beginner - how can I make the camera follow the character but still face the same direction? 2 Answers

camera not working after scripting player state 1 Answer

Cinemachine not showing up in toolbar. 0 Answers

Camera Follows 2D player 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