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
1
Question by vished · Jun 28, 2012 at 07:44 AM · randomlerpvectorlocal

How to 'Lerp' random local vectors? (Solved!)

I want my character to randomly fly around smoothly. I'm at a loss as to why this doesn't work :(

 private var randDir : Vector3;
 private var curDir : Vector3;
 private var oldDir : Vector3;
 private var controller : CharacterController;
 
 function Start() {        
     
     controller = GetComponent(CharacterController);
 
 }
 
 function Update () {
 
     if (curDir == randDir){ 
         oldDir = randDir;
         randDir.x = Random.Range(-1,2);
         randDir.y = Random.Range(-1,2);
     }
     //print ( "T" + randDir.x + "=" + curDir.x + "T" + randDir.y + "=" + curDir.y + "T" + randDir.z + "=" + curDir.z + " Time:" + Time.time);
         
     //controller.Move(transform.TransformDirection(Vector3.Lerp(Vector3(-.1,0,0), Vector3(0,.1,0), Time.time/10) ));
     
     curDir = Vector3.Lerp(oldDir, randDir, Time.time/10);    
     
     controller.Move(transform.TransformDirection(curDir));
     //controller.Move(curDir * Time.deltaTime * 20);
     
     print (curDir.x + " " + curDir.y + " " + curDir.z + " " + Time.time);    

 }
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

3 Replies

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

Answer by vished · Jun 28, 2012 at 02:24 PM

Thanks guys - it works great now!

 private var curDir : Vector3;
 private var oldDir : Vector3;
 private var controller : CharacterController;
 private var timer : float;
 private var curTime : float;
 var changeSpeed : float = 2;
 var speed : float = 5;
 
 function Start() {
     controller = GetComponent(CharacterController);
 }
 function Update () {
     if (curDir == randDir){ 
     //if (Vector3.Distance(curDir, randDir) <= 0.2){     
         oldDir = randDir;
         randDir.x = Random.Range(-1,2);
         randDir.y = Random.Range(-1,2);
         timer = 0;
     }
     timer += Time.deltaTime; 
     curDir = Vector3.Lerp(oldDir, randDir, timer/changeSpeed);
 
 //    print ( " T " + randDir.x + " = " + curDir.x + " T " + randDir.y + " = " + curDir.y + " T " + randDir.z + " = " + curDir.z + " Timer"+  timer +  "Time:" + Time.time);
             
     local = transform.TransformDirection(curDir)/speed;
     controller.Move(local);
 
     print (local.x + " " + local.y + " " + local.z + " " + Time.time);
 
 }
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 diddykonga · Jun 28, 2012 at 07:58 AM

Not exactly sure if this is the problem or not, but you might want to try

 if (curDir.Distance(randDir) <= 0.2){

cause the chances of them every being equal to each other is one and forever lol XD

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 vished · Jun 28, 2012 at 08:10 AM 0
Share

Isn't that the purpose of Lerp, to make them equal? I tried:

if (Vector3.Distance(curDir, randDir) <= 0.2){

The first Lerp works as expected but when the values become near equal the new values are generated but the next Lerps don't use the time value and happen every frame rather than every 10 seconds - I wonder why...

avatar image
0

Answer by Tim-Michels · Jun 28, 2012 at 08:07 AM

I think your main problem is the fact that you're calculating a new random vector each frame. I would use a Coroutine to calculate a new random direction every x seconds, so you get a more fluent movement.

Now you probably have a lot of jitter on your movement (or very little movement), so I would suggest to recalculate a random direction each 0.5 seconds or so, depending on the kind of movement you want.

Good luck ;)

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 vished · Jun 28, 2012 at 08:21 AM 0
Share

The Lerp takes 10 seconds to trigger the generation of a new vector ( the first time).

curDir = Vector3.Lerp(oldDir, randDir, Time.time/10);  
Why is it different the second time? $$anonymous$$aybe you are right, I have to fix this (bug?) by creating a parallel timer for the new vector generation that works in unison with the Lerp timer, or use a yield...?
yield WaitForSeconds(10.0);

avatar image Tim-Michels · Jun 28, 2012 at 08:56 AM 1
Share

Well the third parameter of the Lerp should be a value between 0 and 1.

Since, after 10 seconds, Time.time/10 will be greater than 1, you need to make sure this 'lerp-parameter' stays between 0 and 1.

$$anonymous$$aybe you can add a simple counter, and when the new random vector gets generated, place that timer back to 0.

avatar image vished · Jun 28, 2012 at 02:26 PM 0
Share

Face palm - of course! Cheers!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Stutter During Distance Correction 0 Answers

[QuaternionSlerp] Lookback script 1 Answer

How Can I Linear Interpolate (Lerp) in Local Space? 2 Answers

Calculating a value based on a weighted element within bounds? 1 Answer

How do you make random local velocity for particles? 2 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