- Home /
(2D, C#) Why can't I affect transition parameters!
I'm trying to switch between 3 different character sprites depending on if the player hold down A or D (Walking) or idle. I thought It'd be easy to do through the animator but for some reason I can't control the logic parameters through my C# script.
Through console output i can see the variable in the script changing but not the actual animator parameter.
Been trying to find a solution for over 5 hours now so I'm fairly desperate.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
Animator cAnimator;
// Use this for initialization
void Start () {
cAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
int cDebugRight = cAnimator.GetInteger("ToRight");
int cDebugLeft = cAnimator.GetInteger("ToLeft");
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * 4f * Time.deltaTime);
cAnimator.SetInteger("ToRight", 1);
}
else if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector2.left * 4f * Time.deltaTime);
cAnimator.SetInteger("ToLeft", 1);
}
else
{
cAnimator.SetInteger("ToRight", 0);
cAnimator.SetInteger("ToLeft", 0);
}
Debug.Log("Animator Parameter To Right: " + cDebugRight);
Debug.Log("Animator Parameter To left: " + cDebugLeft);
}
}
Be sure to check all 4 transitions, in and out.
You showed a back transition but mention a forward transition is not even working.
Your script looks fine except you should set the opposite direction to 0 when you hit the direction key (although your animation will only idle in one direction), make sure you set a transition from directly left to right and right to left and define ToRight and ToLeft in all 6 transitions where:
Idle -> Left: ToLeft = 1, ToRight = 0
Idle -> Right : ToLeft = 0, ToRight = 1
Left -> Idle: ToLeft = 0, ToRight = 0
Right -> Idle: ToLeft = 0, ToRight = 0
Left -> Right: ToLeft = 0, ToRight = 1
Right -> Left: ToLeft = 1, ToRight = 0
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to handle multiple animation variants 1 Answer
Display additional animation on top of other animations 0 Answers
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer
Attempting to change AnimatorController during Runtime 0 Answers