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 MadVash2005 · Nov 13, 2012 at 08:52 PM · vector3velocity

Using Velocity to Switch Animations

Good afternoon. Still a Unity n00b here and, I'm trying to do as the subject says using Vector3. Here's a sampling of the script (borrowed from the Lerpz tutorial:

 var topSpeed = 20.0;
 var minSpeed = 1.5;
 var currentSpeed;
 
 function FixedUpdate ()
 {
     currentSpeed = Vector3(Mathf.Lerp(minSpeed, topSpeed, Time.fixedDeltaTime), 0, 0);
     
 }
 
 function Update ()
 {
     var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
      //var currentSpeed = playerController.GetSpeed();
 
     //Fade in gallop
     if (currentSpeed > 12.0)
     {
         animation.CrossFade("gallop");
         animation.Blend("jump", 0);
     }
     // Fade in run
     else if (currentSpeed > 6.0) //playerController.walkSpeed)
     {
         animation.CrossFade("trot");
         // We fade out jumpland quick otherwise we get sliding feet
         animation.Blend("jump", 0);
     }
     // Fade in walk
     else if (currentSpeed > 0.1)
     {
         animation.CrossFade("walk");
         // We fade out jumpland realy quick otherwise we get sliding feet
         animation.Blend("jump", 0);
     }
     // Fade out walk and run
     else
     {
         animation.Blend("walk", 0.0, 0.3);
         animation.Blend("trot", 0.0, 0.3);
         animation.Blend("gallop", 0.0, 0.3);
     }
     
     animation["trot"].normalizedSpeed = trotSpeedScale;
     animation["walk"].normalizedSpeed = walkSpeedScale;
     animation["gallop"].normalizedSpeed = gallopSpeedScale;
     
 }

But at this line of code

 if (currentSpeed > 12.0)

I get a LONG error message that reads:

MissingMethodException: Method not found: 'UnityEngine.Vector3.op_GreaterThan'. Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher () Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create () Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey12.<>m_6 () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) Rethrow as MissingMethodException: Greater than is not applicable to operands 'UnityEngine.Vector3' and 'System.Single'. Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) ThirdPersonPlayerAnimationHorse.Update () (at Assets/Maya_Models/Scripts/ThirdPersonPlayerAnimationHorse.js:59)

So what is it that I'm doing wrong THIS time?

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 Scribe · Nov 13, 2012 at 09:20 PM 0
Share

why is 'currentSpeed' a Vector3?

change the line in fixed update to

currentSpeed = $$anonymous$$athf.Lerp($$anonymous$$Speed, topSpeed, Time.fixedDeltaTime);

and it should work alright, correct me if I am wrong!

Scribe

avatar image MadVash2005 · Nov 13, 2012 at 11:10 PM 0
Share

Well I changed it to what you recommended and I'm no longer getting any of the error messages, but the animations aren't changing the way I wanted them to. In fact, it's not animating at all!

1 Reply

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

Answer by aldonaletto · Nov 13, 2012 at 11:25 PM

The way you're using Lerp will always return a value close to minSpeed, because Time.fixedDeltaTime is about 0.020 (default value).
If you want to go from minSpeed to topSpeed linearly, use something like this:

 ...
 var currentSpeed: float; // define the variable type to avoid problems
 var acceleration: float = 5.0; // how many m/s the speed will grow per second
 
 function Start(){
   currentSpeed = minSpeed;
 }
 
 function Update(){
   currentSpeed += Time.deltaTime * acceleration;
   var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
   ...
Comment
Add comment · Show 5 · 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 MadVash2005 · Nov 14, 2012 at 03:50 PM 0
Share

Thanks but putting this code makes my current speed keep increasing WAY past the top speed of 20. Help!

avatar image Scribe · Nov 14, 2012 at 04:42 PM 2
Share

you can fix that with $$anonymous$$athf.Clamp:

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html

Scribe

avatar image MadVash2005 · Nov 14, 2012 at 05:40 PM 0
Share

Wow! It worked the way I wanted to! Thanks, fellas. You guys are great!

avatar image Scribe · Nov 14, 2012 at 05:50 PM 0
Share

Glad you got it working! $$anonymous$$ake sure to click the tick next to Aldo Naletto's answer, so people know it has been answered and so he gets $$anonymous$$arma! :D

avatar image aldonaletto · Nov 15, 2012 at 12:35 AM 0
Share

$$anonymous$$y bad, I forgot to clamp currentSpeed! $$anonymous$$any thanks to @Scribe for having suggested the right solution.

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

11 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

Related Questions

Velocity value changes way to drastically when position quickly changes 0 Answers

force.normalized * magnitude 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

How can I change vector speed? 1 Answer

How can i make my crosshair drag behind the camera? 0 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