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 Clivens · Apr 02, 2015 at 12:39 PM · movementtransformlerp

How do I smooth the movement of three transforms moving at different rates along a single axis?

I'm moving three cubes along their Z axis by their transform component at different rates while having a camera following one of them. alt text

How would I be able to rid of the jerkiness that represents from the other two if they are moving at random rates every update to have all three cubes appear smooth?

 using UnityEngine;
 using System.Collections;
 
 public class CarControllerAI : MonoBehaviour {
     private float speed;
     private Transform _endPoint;
 
     void Start() {
         //The point we want our transform to reach.
         _endPoint = GameObject.FindGameObjectWithTag("FinishLine").GetComponent<Transform>();
         //The speed from .1 to 1 for our lerp
         speed = Random.Range(.1f, .5f);
     }
 
     void Update() {
         speed = Random.Range (.1f, .5f);
     }
 
     void FixedUpdate() {
         transform.position = Vector3.Lerp (transform.position, new Vector3(transform.position.x, transform.position.y, _endPoint.position.z), speed * Time.deltaTime);
     }
 }

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
1
Best Answer

Answer by lordlycastle · Apr 02, 2015 at 10:00 PM

I would recommend you not use Lerp function to move, as long as you have other options. Most people don’t really understand and freak when get unexpected behaviour. @kingcoyote’s solution will wok if you are using constant speed, but still I wouldn’t use Lerp. Anyways, if you don’t want to use constant speed then you can use SmoothDamp to move. In that you’ll have to set the smoothTime using your previous velocity and present velocity.

 Vector3 lastSpeed = Vector3.zero;
 Vector3 currentSpeed = Vector3.zero;
 
 void Update(){
     float newSpeedMagnitude = Random.Range(1, 5);
     var deltaTranform = Mathf.Abs((currentSpeed * newSpeedMagnitude - lastSpeed)) * Time.deltaTime;
     tansform.positon = Vector3.SmoothDamp(tansform.positon, transform.positon + deltaTranform, currentSpeed, Time.deltaTime);
     lastSpeed = currentSpeed * newSpeedMagnitude;
 }

I do have to warn you that this isn’t a good way to move either. It’s just suitable for you situation where you are randomly change speed. You probably shouldn’t use random speed, that does give it odd behaviour.

Hope this works out, I haven’t tried it. Comment if it does or not.

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 Clivens · Apr 03, 2015 at 03:15 AM 0
Share

This is what gave me the best results, completely smooth and random.

 using UnityEngine;
 using System.Collections;
 
 public class CarControllerAI : $$anonymous$$onoBehaviour {
     private Transform target;
     private float smoothTime = .5f;
     private float yVelocity = 0.0f;
 
     void Start() {
         target = GameObject.FindGameObjectWithTag("FinishLine").GetComponent<Transform>();
     }
 
     void Update() {
         float speed$$anonymous$$od = Random.Range(0f, 20f);
         float newPosition = $$anonymous$$athf.SmoothDamp(transform.position.z, target.position.z, ref yVelocity, 20f % speed$$anonymous$$od, 20f);
         transform.position = new Vector3(transform.position.x, transform.position.y, newPosition);
     }
 }

Your code snippet didn't really work at all for me :/

avatar image
0

Answer by kingcoyote · Apr 02, 2015 at 09:30 PM

First off, if they change speed every frame, of course it's going to be jerky! Is that what you really wanted, or did you want each object to move steadily at a random rate that is defined on start?

Second, you might want to change how you are using Vector3.Lerp. That might be a part of your problem. Rather than doing a small, almost fixed size linear interpolate between current position and end position, you should linear interpolate between start and end, and smoothly grow the intermediate value. Like this:

  using UnityEngine;
  using System.Collections;
 
  public class CarControllerAI : MonoBehaviour {
      private float speed;
      private float _location;
      private Transform _startPoint;
      private Transform _endPoint;
 
      void Start() {
          //The point we want our transform to reach.
          _endPoint = GameObject.FindGameObjectWithTag("FinishLine").GetComponent<Transform>();
          // The starting point
          _startPoint = transform;
          // the speed should be the number of 1/s where s is the 
          // number of seconds needed to get to the end.
          speed = 1 / Random.Range(5, 10);
          // default the location to 0
          _location = 0;
      }

      void Update() {
          // increment the location based on speed. 
          _location += speed * Time.deltaTime;
          transform.position = Vector3.Lerp(
              _startPoint.position, 
              _endPoint.position), 
              _location);
      }
  }


Comment
Add comment · Show 2 · 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 Clivens · Apr 02, 2015 at 11:17 PM 0
Share

Your solution worked well, some syntax errors; also noticed that it doesn't implicitly widen integers to floats in this compiler? So your 5 and 10 arguments were truncating to 0 :/. Weird stuff, but I have them smoothly moving to a point, yet I'm going to try the other one as well to get random speeds every frame.

 using UnityEngine;
 using System.Collections;
 
 public class CarControllerAI : $$anonymous$$onoBehaviour {
     private float speed;
     private float _location;
     private Transform _startPoint;
     private Vector3 _endPoint;
 
     void Start() {
         // The starting point
         _startPoint = transform;
         //The point we want our transform to reach.
         _endPoint = new Vector3(_startPoint.position.x, _startPoint.position.y, GameObject.FindGameObjectWithTag("FinishLine").GetComponent<Transform>().position.z);
         // the speed should be the number of 1/s where s is the 
         // number of seconds needed to get to the end.
         speed = 1 / Random.Range(20f, 60f);
         // default the location to 0
         _location = 0;
     }
     
     void Update() {
         // increment the location based on speed. 
         _location += speed * Time.deltaTime;
         transform.position = Vector3.Lerp(_startPoint.position, _endPoint, _location);
     }
 }
avatar image kingcoyote · Apr 02, 2015 at 11:51 PM 0
Share

Sorry about the compiler errors. I'm not in front of Unity at the moment and had to free hand that. And yeah, the range would truncate to 0 since 1/5, if both are cast as integer types, rounds down to 0. It could also be fixed by changing the 1 to 1.0 or 1f.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Add specific movement on double click 1 Answer

Learning to move 1 Answer

Unity 2D Scripted Movement 2 Answers

Add Force To One Object in Relation to the Rotation of Another Object (C#) 1 Answer

Help using arrow keys to move 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