- Home /
HELP!! Animation wont reverse..
Hi all,
followed the forums & answers, but i still could get it run animation in reverse... animation was done in unity, everytime i run, it would only run the 1st step, then when i click again, supposely it should reverse but it repeated again...
any ideas anyone?
Thanks
animation.Stop();
function Update () {
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
hit.collider.gameObject.animation["slot1"].time=0.0;
hit.collider.gameObject.animation["slot1"].speed=1.0;
hit.collider.gameObject.animation.Play ("slot1");
//hit.collider.gameObject.animation.Stop ("slot1");
Debug.Log("open "+hit.collider.name);
}
}
else if (Input.GetMouseButtonDown(0))
{
//if (Physics.Raycast(ray, hit))
//{
hit.collider.gameObject.animation["slot1"].speed=-1.0;
hit.collider.gameObject.animation["slot1"].time = hit.collider.gameObject.animation["slot1"].length;
hit.collider.gameObject.animation.Play("slot1");
//hit.collider.gameObject.animation["slot1"].time=0;
//hit.collider.gameObject.animation["slot1"].speed=1;
//hit.collider.gameObject.animation.Play ("slot1");
Debug.Log("close "+hit.collider.name);
//}
}
}
Answer by DaveA · Jan 21, 2011 at 08:28 AM
You're testing for the same mouse event in the same frame. I think you need a boolean or something, toggle that, and depending on current value, run the anim forward or backward.
Something like this:
var forward = true; function Update () {
if (Input.GetMouseButtonDown(0)) // user has pressed left mouse button
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) // hit object
forward = !forward; // reverse direction
if (forward)
{
hit.collider.gameObject.animation["slot1"].time=0.0;
hit.collider.gameObject.animation["slot1"].speed=1.0;
hit.collider.gameObject.animation.Play ("slot1");
Debug.Log("open "+hit.collider.name);
}
else // backward
{
hit.collider.gameObject.animation["slot1"].speed=-1.0;
hit.collider.gameObject.animation["slot1"].time = hit.collider.gameObject.animation["slot1"].length;
hit.collider.gameObject.animation.Play("slot1");
Debug.Log("close "+hit.collider.name);
}
}
ehh.... actually, i want to left to run then left click to reverse, how should i do so?
animation will be run one time when button click??
let's say for example, i got 3 slots are same on X,Y axis but different on Zaxis. my qn: is that how do i do it if user click on slot1, but disable raycast on slot2 & 3? then click on slot2, disable 1&3?
thanks again