- Home /
Drag mouse Up or Down to play or reverse animation
Hi guys! I think this should be a simple question but I can´t find a simple answer:
I already have an animated 3D model with a collider. The animation is "legacy". I made it in Cinema 4D and it´s included in the 3D model. All I want to do is, when the user clicks and drags down on the object (with a desktop mouse), the animation plays forward, and when they drag up, the animation plays backwards. It is a cord that needs to be pulled to disengage something.
I already have two functions that play the animation forward and backward and they work corectly. I tested them with a simple Key Press (if the user presses "P" the animation moves forward, and when they press "O" it moves backwards). "TrolleyAnim" is an Animation I have already asigned which includes the "Disconect" animation clip. All that works fine.
private float animTime = 0;
void AnimGoForth()
{
if (animTime< 4.5f)
{
TrolleyAnim["Disconect"].time = animTime;
TrolleyAnim["Disconect"].speed = 0.0f;
TrolleyAnim.Play("Disconect");
animTime += 0.1f;
}
}
void AnimGoBack()
{
if (animTime> 0.0f)
{
TrolleyAnim["Disconect"].time = animTime;
TrolleyAnim["Disconect"].speed = 0.0f;
TrolleyAnim.Play("Disconect");
animTime-= 0.1f;
}
}
It works quite Ok with the keyboard inputs, I just need to do the same with OnMouseDrag.
An important element is that I need to call this from a script that is not attached to the GameObject that has the collider to drag on, so I need to specify in some way which collider am I dragging on and if I am dragging up or downwards.
Thanks a lot in advance.