Issue with using animationcurve for button click animation [Unity]

Home / Uncategorized / Issue with using animationcurve for button click animation [Unity]

I’ve got a button that animates on click and uses an animationcurve for the click animation. Works great.

Problem is, I’m screwing up somewhere in the coroutine and it causes the object the script is applied to, to snap back to world 0 when I click on it. How do I move the object based on the curve wherever the object happens to be in world space, and not pop to world 0 on first click?

Here’s my button script:using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button : MonoBehaviour {

//create a placeholder for the animation coroutine
IEnumerator currentCoroutine;

//create public animation curve for adjustments
public AnimationCurve curveAnim;

public float posDisplacement; //animation value (movement distance, opacity, etc.)
public float speed; //the speed at which the animation curve should complete in (like a magnitude)

//animaiton coroutine
IEnumerator kickback(){
float curveTime = 0f; //beginning time of the animation curve
float curveAmount = curveAnim.Evaluate (curveTime); //a place to store the value of the curve at any point in time

//while loop to execute the curve until it completes
while (curveTime < 1.0f) { curveTime += Time.deltaTime * speed; //reference curve at time - speed affects the duration at which the curve completes curveAmount = curveAnim.Evaluate (curveTime); //store the current curve value from the time * speed //do something using the value of the curve at time //this will move the current object in z in reference to the animaiton curve by multiplying the value of the curve by the animation value transform.position = new Vector3(transform.position.x, transform.position.y, curveAmount*posDisplacement); ] //break after completing yield return null; } } //Button events void OnTouchDown(){ //Debug.Log ("Touch Down!"); } void OnTouchUp(){ //check if a coroutine is already running. If so, stop it and then start it again if (currentCoroutine != null) { StopCoroutine (currentCoroutine); } currentCoroutine = kickback(); //assign what the currently running coroutine is StartCoroutine (currentCoroutine); //run the current coroutine OnTouchUp //Debug.Log ("Touch Up!"); } void OnTouchStay(){ //Debug.Log ("Staying..."); } void OnTouchExit(){ //Debug.Log ("Exited"); } }


Read more

Leave a Reply

Your email address will not be published. Required fields are marked *