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 phxvyper · Jul 19, 2013 at 03:59 AM · cameramovementlerpslerpsmoothing

Movement Smoothing Script not Working

I've created the following movement smoothing script:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothedCamera : MonoBehaviour {
 
     public Transform Following;
     public float positionSpeed = 3f;
     public float rotationSpeed = 3f;
     private float startTime = 0f;
     private float journeyLength = 0f;
     private float rotationLength = 0f;
     
     void Start() {
         startTime = Time.deltaTime;
         journeyLength = Vector3.Distance(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z));
         rotationLength = Vector3.Distance(transform.rotation.eulerAngles, new Vector3(Following.rotation.x + 25, Following.rotation.y + 270, Following.position.z));
     }
 
     void Update()
     {
         float distCovered = (Time.deltaTime - startTime) * positionSpeed;
         float fracJourney = distCovered / journeyLength;
         
         if (transform.position != Following.position)
             transform.Translate(Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));
 
         float rotCovered = (Time.deltaTime - startTime) * rotationSpeed;
         float fracRot = rotCovered / rotationLength;
 
         if (transform.rotation != Following.rotation)
             transform.Rotate(Quaternion.Slerp(transform.rotation, new Quaternion(Following.rotation.x + 25, Following.rotation.y + 270, Following.position.z, Following.rotation.w), fracRot).eulerAngles);
     }
 }

 

This class is supposed to smoothly translate the camera its attatched to based on a Parent Transform. This parent transform will be assigned to an empty object that is at the center of a moving vehicle.

The reason i add (5, 5, 0) to the position and (25, 270, 0) to the rotation is so that the camera will float above and behind the vehicle while still looking at it.

The camera doesn't do anything and the error console is filled with hundreds of messages.

What did i do wrong?

Comment
Add comment · Show 2
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 robertbu · Jul 19, 2013 at 07:12 AM 0
Share

Try:

 transform.position = Vector3.Lerp(transform.position, ParentTransform.position, Time.deltaTime * positionSpeed);
 transform.rotation = Vector3.Slerp(transform.rotation, ParentTransform.rotation,  Time.deltaTime * rotationSpeed);

You may have to significantly change the value of 'rotationSpeed'. Note this code assumes that 'ParentTransform' is not a true parent of this game object. A parent/child movement would be solved somewhat differently.

avatar image phxvyper · Jul 19, 2013 at 08:03 PM 0
Share

The camera is not a child/parent of the empty object that represents where it's final position is. What do you mean when you say that i have to significantly change the value of rotationSpeed?

2 Replies

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

Answer by Eric5h5 · Jul 19, 2013 at 04:07 AM

It doesn't really make much sense to put a Lerp inside Translate, or a Slerp inside Rotate. Do one or the other, not both.

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 phxvyper · Jul 19, 2013 at 04:12 AM 0
Share

I've adjusted my code to use only Lerp. The same issue arises. Can you shed some light on why i need to use only one of these functions?

avatar image
0

Answer by RyanZimmerman87 · Jul 28, 2013 at 01:29 AM

Hmm in mine I use Time.time not Time.deltaTime not sure if that could cause any problems for you.

I think you will also need a different way to collect the time. Using only the start time to subtract from the current time seems like it would only work for the original movement. I believe you will need to grab a new startTime every time the player moves and use that start time until the camera reaches your desired position. Otherwise the % of distance traveled will get all messed up. It needs to be for the current distance traveled for any camera movement until it catches up. So don't reset the time until camera comes to rest and player moves again.

In my version your code: transform.Translate(Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));

Would be: transform.position = Vector3.Lerp(transform.position, new Vector3(Following.position.x + 5, Following.position.y + 5, Following.position.z), fracJourney));

Not sure if the transform.translate wouldn't work as well as what I use transform.position. I would also convert your Following.position.x stuff to something like followingTransform.position. That will make it a bit easier to work with probably but shouldn't change the result.

I also create a vector position for where the camera should be currently looking using the current desired look position in relation to the current -transform.position and I use .normalize here.

I then use that new vector position for a Quaternion.LookRotation.

And finally use the Quaternion.LookRotation for Quaternion.Slerp very similar to what the guy above posted.

Hopefully that helps! I'm not sure how clear that was. The problem with this kind of thing is that the way your character moves is most likely completely different than mine but I think if you can get the time thing sorted out it should work better!

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

17 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

Related Questions

How do I translate around a circle? 3 Answers

[QuaternionSlerp] Lookback script 1 Answer

Camera Movement Smoothing 1 Answer

Camera Stutter During Distance Correction 0 Answers

Move A Camera Between Two Points Smoothly 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