Hey guys, I was wondering if any of you could help me out. I would like to reverse my opening chest animation for when my chest closes again. How could i accomplish that ?
This is the code i use now.
using UnityEngine;
using System.Collections;
public class ClickMe : MonoBehaviour {
private bool doorOpen = false;
private float animationFinished = 0;
public void OnMouseUp()
{
Debug.Log ("Ive been clicked");
if(Time.time > animationFinished)
{
if(doorOpen)
{
GetComponent<Animation>().Play("ChestAnim");
}
else
{
GetComponent<Animation>().Play("ChestAnim");
}
animationFinished = Time.time + 1;
doorOpen = !doorOpen;
}
}
}
fafase
November 4, 2015, 12:39pm
2
Animation animation = GetComponent();
animation[clipName].normalizeTime = 1f;
animation[clipName].speed = -1f;
animation.Play();
Completed Script:
using UnityEngine;
using System.Collections;
public class ClickMe : MonoBehaviour {
private bool IsOpen = false;
public float distanceToActivate = 15;
private float animationFinished = 0;
public GameObject ChestPrefab;
Animation ChestAnim1;
GameObject player;
float distance;
public void Awake(){
player = GameObject.FindGameObjectWithTag("Player");
}
void Update(){
distance = Vector3.Distance (this.transform.position, player.transform.position);
}
public void OnMouseUp()
{
if (distance <= distanceToActivate) {
if (Time.time > animationFinished) {
if (IsOpen) {
Debug.Log ("Closing Chest");
ChestAnim1 = ChestPrefab.GetComponent<Animation> ();
ChestAnim1 ["ChestAnim"].normalizedTime = 1f;
ChestAnim1 ["ChestAnim"].speed = -1f;
ChestAnim1.Play ("ChestAnim");
} else {
Debug.Log ("Opening Chest");
GetComponent<Animation> ().Play ("ChestAnim");
}
animationFinished = Time.time + 1;
IsOpen = !IsOpen;
}
} else {
Debug.Log ("Player Too Far");
}
}
}