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 /
avatar image
0
Question by Krumpet · Oct 05, 2014 at 09:10 PM · c#smoothfollowsmooth follow

C# Smooth Follow Script Causes Camera "Jitter"

With the help of a few camera scripting tutorials I managed to write up a script for the camera that lets you rotate it around a character when you hold down the left mouse button, but also has the functionality of the Smooth Follow javascript that comes in unity's scripts package.

The only problem is that it causes a jitter with the rotation and y position of the camera every frame. The jitter is usually less than 1, but it is noticeable. I'm still learning some of the functionality of Mathf.Lerp and Quaternions, so I don't really know what the problem is.

Here's the code, if anyone can help me figure it out:

using UnityEngine; using System.Collections;

public class WitchCamera : MonoBehaviour

{

 // The target we are following
 public GameObject target;
 // the height we want the camera to be above the target
 public float height = 5f;
 // The distance in the x-z plane to the target
 public float distance = 10f;
 //public float frameOffset = 5f;
 public float rotateSpeed = 5f;
 // How much we 
 public float heightDamping = 2f;
 public float rotationDamping = 3f;

// Vector3 offset;

 void Start() 
 {
 }
 
 void LateUpdate() 
 {
     // Early out if we don't have a target
     if (!target)
         return;

     float wantedRotationAngle = target.transform.eulerAngles.y;
     float wantedHeight = target.transform.position.y + height;
     
     float currentRotationAngle = target.transform.eulerAngles.y;
     float currentHeight = target.transform.position.y;

     //Rotates target attached to play character if the left mouse button is pressed.
     if(Input.GetButton("Fire2"))
     {
         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
         target.transform.Rotate(0, horizontal, 0);

     }
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
     
     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

     // Convert the angle into a rotation
     Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     transform.position = target.transform.position;
     transform.position -= currentRotation * Vector3.forward * distance;
     
     // Set the height of the camera

     Vector3 temp = transform.position;// copy to an auxiliary variable...
     temp.y = currentHeight;// modify the component you want in the variable...
     transform.position = temp;// and save the modified value 
     
     // Always look at the target
     transform.LookAt(target.transform);

 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

3D Gravity towards one object 3 Answers

How to Load from an external file, sound 0 Answers

Clamping a Manipulated Camera 0 Answers

c# Adjust In-Game audio 1 Answer

How do i learn C# or UnityScript as a teen? 6 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