- Home /
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?
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
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!
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);
...
Thanks but putting this code makes my current speed keep increasing WAY past the top speed of 20. Help!
you can fix that with $$anonymous$$athf.Clamp:
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
Scribe
Wow! It worked the way I wanted to! Thanks, fellas. You guys are great!
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
$$anonymous$$y bad, I forgot to clamp currentSpeed! $$anonymous$$any thanks to @Scribe for having suggested the right solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083822im_/https://answers.unity.com/themes/thub/images/avi.jpg)