• 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 Kulahan · Aug 16, 2011 at 08:52 PM · camerajitter

stupid camera jitter.

HOKAY, so here's my issue. In the following code, I'm having an issue where I don't want to use the smooth camera (mostly because I hate the look of it), so I found some nice code for a camera that "slides" into place whenever you turn. It really looks quite nice when you're turning. Unfortunately, whenever you stop moving, the camera still attempts to slide into place, even though there's nowhere to slide. This causes it to slowly flip-the-hell-out, shooting from side to side. It also has the issue that when you first get into car, it's always facing to the side, rather than just automatically setting behind the car. Halp plz!

 public class CarCamera : MonoBehaviour
 {
     public Transform target = null;
     public float height = 1f;
     public float positionDamping = 3f;
     public float velocityDamping = 3f;
     public float distance = 4f;
     public LayerMask ignoreLayers = -1;
 
     private RaycastHit hit = new RaycastHit();
 
     private Vector3 prevVelocity = Vector3.zero;
     private LayerMask raycastLayers = -1;
     
     private Vector3 currentVelocity = Vector3.zero;
     
     void Start()
     {
         raycastLayers = ~ignoreLayers;
     }
 
     void FixedUpdate()
     {
         currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
         currentVelocity.y = 0;
         prevVelocity = currentVelocity;
     }
     
     void LateUpdate()
     {
         float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
         camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor);
         float currentDistance = Mathf.Lerp(7.5f, 6.5f, speedFactor);
         
         currentVelocity = currentVelocity.normalized;
         
         Vector3 newTargetPosition = target.position + Vector3.up * height;
         Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
         newPosition.y = newTargetPosition.y;
         
         Vector3 targetDirection = newPosition - newTargetPosition;
         if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
             newPosition = hit.point;
         
         transform.position = newPosition;
         transform.LookAt(newTargetPosition);
         
     }
 }
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 roamcel · Aug 17, 2011 at 06:17 AM

I suspect that this might be a "physics" issue rather than a camera issue. You should assure that's the case or not by attaching the camera script to a static object in the scene.

If the issue is physics related (it's your camera's target jittering, not the camera), you'll need to tweak your camera target's motor. On the other case, you might just need a dampener value for your camera interpolations which are possibly better off like

                                                           vvv
camera.fieldOfView = Mathf.Lerp(55, 72, speedFactor * Time.deltaTime);
float currentDistance = Mathf.Lerp(7.5f, 6.5f, speedFactor * Time.deltaTime);

oh and regarding the camera's initial rotation: let's just solve one problem at a time

Comment
Add comment · Show 3 · 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 Kulahan · Aug 17, 2011 at 02:03 PM 0
Share

I'm pretty sure you were right. Now, I was a bit lazy and tested this by setting the .is$$anonymous$$inematic setting to true on the car, making it completely immobile. This would have the same effect as attaching it to a static object, correct? If it is, then I'm a bit curious as to what sort of tweaks I would make to the car's motor. Also, haha, sorry in advance if these are dumb questions - I'm new to program$$anonymous$$g, and was just sorta thrown into this project, so it's a mildly daunting thing for me right now - still trying to get used to the whole "using a game engine" thing!

avatar image roamcel · Aug 17, 2011 at 04:05 PM 0
Share

heh, yeah, setting iskinematic is equivalent to reattaching the camera. regarding the car's motor fix, you need be sure that rigidbodies have proper weights in your gameobject. One thing that's commonly ignored is that the $$anonymous$$ASS parameter is in kilograms, and if all your rigidbodies have 1 mass they actually weigh 1 kilogram! Of course you need to properly set all vehicle values to be sure that the engine has no trobule stabilizing. So you can try to set the wheels to, say, 2.5 mass, and the vehicle to 100 mass (docs suggest to avoid mass differences of more than 100 units).

avatar image Kulahan · Aug 17, 2011 at 07:20 PM 0
Share

Interestingly enough, I'm not 100% sure that anything on the car other than the wheels even has a mass. They've all got a mass of 3 themselves, but I wonder if that's the issue?

avatar image
0

Answer by Wolfram · Aug 17, 2011 at 02:59 PM

If your currentVelocity is 0, newPosition and newTargetPosition will refer to the same location. At the end of your code you set the transform's position to newPosition, and then try to look at newTargetPosition, which in this case, is the same point.

I am not sure how transform.LookAt() behaves in such a case, but in theory it should be undefined, because it can't determine a viewing direction if your position and the lookat-target are identical.

Similarly, I can well imagine that transform.LookAt() will start to behave unpredictable and jerky once currectVelocity approaches zero (for example if the car slows down and stops), since the length of the viewing direction vector, required to determine the transform's rotation, will also approach zero.

A solution to this dilemma could be, to define a "forward" direction, and always subtract this forward vector (or a fraction of it) from newTargetPosition. This will not only prevent the zero-length problem, but also solve the issue of initial orientation, and making sure that your follower will always adjust itself directly behind your object, looking towards it, once your object becomes static.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I fix this jitter problem? 0 Answers

3rd person platformer OrbitCamera.js : making offset local space causes jitter 0 Answers

Uneven camera movement during roll 0 Answers

Hey, is there a way to reduce or adjust this edge issue as shown in the picture? 0 Answers

Camera jitter when using Lerp/Slerp 1 Answer


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