Search Unity

Trouble with scripting the movement of my 2D blend tree

Discussion in 'Scripting' started by LeftyTwoGuns, Apr 22, 2014.

  1. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    I created a 2D blend tree that blends a forward and backward walk animation with a left and right sidestep animation. It works in the preview window but it's not working in-game. Here is a screen of the blend tree:

    $BlendTree.png

    Here is my current script:

    Code (csharp):
    1. public class PlayerControllerScript : MonoBehaviour {
    2.  
    3.     Animator anim;
    4.    
    5.     // Use this for initialization
    6.     void Start () {
    7.        
    8.         anim = GetComponent<Animator>();
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.         float move = Input.GetAxis("Vertical");
    16.         float sidestep = Input.GetAxis("Horizontal");
    17.         anim.SetFloat("Speed", move);
    18.         anim.SetFloat("Direction", sidestep);
    19.        
    20.     }
    21. }
    22.  
    23.  
    In the Animator, Speed and Direction are increasing and decreasing correctly with the right input but my character is not moving. Is there a problem with my script, my blend tree, or both?
     
  2. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    I've been looking at some other examples, trying to get a better understanding.

    If VelocityX (horizontal movement) and VelocityZ (vertical movement) are the two parameters in my 2D blend tree, I have to calculate these two parameters and apply them to horizontal and vertical inputs? Am I getting close to understanding this?