Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Animation controlling movement....

Discussion in 'Animation' started by codejoy, Nov 23, 2013.

  1. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    I broke down and bought a character that animates from the asset store so I could get around the programmer art I had for spheres, and capsules. So this works okay, after some tweaking I got the lovely model rotated the right direction, and looking okay..set up right and wanted to do more than the constant run animation.

    So I built this little script that says "when the player attacks", CrossFade animation:

    anim.CrossFade("Staff Spell Forward");

    and when they move in a direction:
    anim.CrossFade("Walk");

    etc... well the problem is it seems my code that was moving the spehere from one "tile" to the next, happens to fast. and the attack "the sphere darts out hits enemy darts back" happens WAY to fast to even see the animation start.

    This is new ground to me, should i set how fast the enemy "fires" his attack and walks between tiles based on the animation? before it was just based on "what felt" right in lerping between two positions:


    basically if the character is moving i currently say this:
    Code (csharp):
    1. void Update() {
    2.    if(moving){
    3.         weight += Time.deltaTime * speedAdjust;
    4.         this.transform.position = Vector3.Lerp(startPosition, targetPosition, weight);
    5.         this.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, weight);
    6.    }

    elsewhere in the update i calculate if they hit the right key for instance, where the targetPosition is:

    Code (csharp):
    1.    if(Input.GetKeyDown("right"))
    2.             {
    3.              
    4.                     startPosition = this.transform.position;
    5.                                            targetRotation = Quaternion.Euler(0f, -270f, -90f);
    6.                    
    7.                     targetPosition = new Vector3(this.transform.position.x + 1.5f, this.transform.position.y, this.transform.position.z);
    8.                     moving = true;
    9.                     lastDir = "right";
    10.                     }
    where speedAdjust is global float at:
    public float speedAdjust = 10f;

    So for the sphere this worked, but now when I try to animate the player (i actually attach his position to the spheres, cause all the brains of the hit detection is under the sphere still)... so should I change how the speedAdjust works to work more off a function of how the player is animating...and somehow run the animation faster or do the movement lerping over the whole time of animation, I am soooo 3d stuipd in this area...was all fun and games with the sphere and capsules. Advice?

    NOTE: I forgot to add there is a snippet of code that just says the moving is done if the current position = start position...
     
    Last edited: Nov 23, 2013