Confused on Animation and Animator.

Alright, so I’ve just started with unity yesterday. I’ve made a little animation to see how it all works, this is what it looks like.

I set the Y velocity to 1 and attached an animator, a rigid body and a script. So, that was simple. I wanted to improve it a bit, I wanted it to make like the dog -or whatever that horrid thing is- leap.

It’s a 3 key-frame animation. So, I just decided the best way to do this was to speed up the velocity after 2 of the 3 keyframes and then slow it down. That’s probably a bad way, but that’s one I can think of.

So, I set out on a journey to get the animation time, which is in AnimationState. I could get the AnimationClip, and the AnimationStateInfo, but can’t get AnimationState. So, I gave up on this method.

Second method! I ditched the Animator component altogether and decided to do the animating myself. An even more terrible idea, but what the hell have I got to lose?

So, I’ve created a function that gets called every second, that function will swap the sprite and add some velocity that later can be reduced in order to simulate the ‘leaping’ motion.

The code is unfinished in terms that I didn’t include the slowing down of the velocity, because I ran into a problem.

var sprite : SpriteRenderer;
var Sprite1 : Sprite;
var Sprite2 : Sprite;

var timerInterval = 1;
var timerNextTrigger = 0.0f;

function Update () {
    if(Time.time >= timerNextTrigger){
        timerNextTrigger = Time.time + timerInterval;
        executeTimer();
    }

  
    if(Time.time > 3){
        sprite.sprite = Sprite2; //Just to see if it works, it does.
    }
}

function executeTimer(){
    leap();
}

function leap(){
    rigidbody2D.velocity.y = 2;
   
   
    Debug.Log(sprite.sprite.GetInstanceID()); //Prints 58 all the time
    Debug.Log(Sprite1.GetInstanceID()); //Prints 48
    Debug.Log(Sprite2.GetInstanceID()); //Prints 58

    if(sprite.sprite.GetInstanceID == Sprite1.GetInstanceID()){//I've tried .name as well.
        sprite.sprite = Sprite2;
    }else{
        sprite.sprite = Sprite1;
    }
}

The code doesn’t work. I don’t know why. It’s probably due to something really stupid and minor like it always is.

But, I want to know what would be the best way of achieving that ‘leaping’ motion. I think my 2 ways are kind of bad. How would you do it?

Thanks!

Bump of hope!

We have progress ladies and gentlemen! I was missing a parenthesis. I’m pretty sure nobody is interested, but here’s the code.

var sprite : SpriteRenderer;
var Sprite1 : Sprite;
var Sprite2 : Sprite;

var timerInterval = 1;
var timerNextTrigger = 0.0f;

var slowDown = false;
var slowDownValX = 6.0;
var speed = 1; 
var vector : Vector2;

function Update () {
    if(Time.time >= timerNextTrigger){
        timerNextTrigger = Time.time + timerInterval;
        executeTimer();
    }
 
    if(slowDown){
        slowDownValX = slowDownValX - 0.03;
        var slowDownVal = Mathf.Pow(slowDownValX, 2) / 10;
     
        rigidbody2D.velocity.x = vector.x * speed * slowDownVal;
          rigidbody2D.velocity.y = vector.y * speed * slowDownVal;
    }
}

function executeTimer(){
    leap();
}

function leap(){
    var pi180 = Mathf.PI / 180;
 
    vector = Vector2(
    -Mathf.Sin(GetComponent(Rigidbody2D).rotation * pi180),
    Mathf.Cos(GetComponent(Rigidbody2D).rotation * pi180));
 
    vector.Normalize();
    rigidbody2D.velocity.x = vector.x * speed;
    rigidbody2D.velocity.y = vector.y * speed;
 
    slowDownValX = 6;
    slowDown = true;
 
    if(sprite.sprite.GetInstanceID() == Sprite1.GetInstanceID()){
        sprite.sprite = Sprite2;
    }else{
        sprite.sprite = Sprite1;
    }
}