- Home /
Play animation as the value changes from 0 to 1 with changing GameObject's position in unity
I want to progress an animation in such a way that when my controller gameobject (say a cube) moves from 0 to 1 unit distance in y axis. 0 being the starting position of the animation and 1 being the end position of the animation. If the gameobject is at 0.5 unit then the animation should be half played and if we go back to 0th position from 0.5 unit then the animation should play in reverse.
My code:
public bool x;
public bool y;
public Transform target;
public Animator animdown;
void Start()
{
target = target.GetComponent<Transform>();
x = true;
}
// Update is called once per frame
void Update()
{
Vector3 movement = new Vector3(0f, Input.GetAxis("Vertical"), 0f);
target.position += movement;
if (target.transform.position.y < 1.0f)
{
y = true;
}
if (target.transform.position.y > 1.0f)
{
y = false;
}
//var pos = arrow.transform.position(vec);
if (x == true)
{
animdown.SetBool("down", true);
}
if (y == true)
{
animdown.SetBool("up", true);
}
if (y == false)
{
animdown.SetBool("up", false);
}
}
Comment