- Home /
How do I access the animator parameter through C# script ?
Hello friends,
Game: This is a 2D game in which according to the swipe direction(4 cardinal direction) the character will move and reach the destination.
Problem: I want the character to move indefinitely in a particular swipe direction without stopping. But in the following code the character moves only after each swipe(in this case I have tried only for right swipe just to test) and not continuously.
// Update is called once per frame void Update () { //Check if Input has registered more than zero touches if (Input.touchCount > 0) { //Store the first touch detected. Touch myTouch = Input.touches[0];
//Check if the phase of that touch equals Began
if(myTouch.phase == TouchPhase.Began)
{
//If so, set touchOrigin to the position of that touch
touchOrigin = myTouch.position;
}
//If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
//store the postion of the touch into touchEnd vector2
Vector2 touchEnd = myTouch.position;
//Calculate the difference between the beginning and end of the touch in x-axis
float x = touchEnd.x - touchOrigin.x;
//Calculate the difference between the beginning and end of the touch in y-axis
float y = touchEnd.y - touchOrigin.y;
//touchOrigin.x to -1 so that the next frame should not execute the if else
touchOrigin.x = -1;
//check if it is a horizontal(x axis is greater) or vertical(y axis is greater) swipe
if(Mathf.Abs(x) > Mathf.Abs(y))
{
//if x is greater than than 0, set horizontal value to 1 else -1
horizontal = x > 0 ? 1 : -1;
if (horizontal == 1)
{
animator.SetInteger("direction" , 4);
int dir = animator.GetInteger("direction");
if(dir == 4)
gameObject.transform.Translate(Vector2.right * 4.0f * Time.deltaTime);
}
else if (horizontal == -1)
{
animator.SetInteger("direction" , 3);
}
}
else
{ vertical = y > 0 ? 1 : -1;
if (vertical == 1)
{
animator.SetInteger("direction" , 1);
}
else if (vertical == -1)
{
animator.SetInteger("direction" , 2);
}
}
}
}
}![alt text][1]
Please tell me how do I access the animator parameter "direction" values in this case to use conditions in the script. Please find the attachment of the animator.
[1]: /storage/temp/53297-animator.png