- Home /
Animator End Frame issues
I have a script that controls a target animation by mouse movement on the screen. It does the controlling part just fine. However an issue arises when you hold the mouse too long in a direction where the animation has stopped.
Example: A gas pedal moves in and out. When the mouse moves up and pedal goes in and when the mouse moves down it goes out. The issue arises when the user holds the mouse in the up/down position even after the animation is done. Then when they try to go in the other direction the animation has to scrub back to that point.
This is all using Animator (I inherited this code from a colleague who was indisposed during production, so It is still kinda new to me).
My question: Is there a simple way to check and see if the animation is at its end (without using animation trigger events) and tell it to stop its counting?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LeverControl : MonoBehaviour {
public Vector2 mouseDownPoint;
public Vector2 mouseDragPoint;
public float yDif;
private RaycastHit hit;
public bool dragging;
public bool atStart;
public bool atEnd;
public bool linear;
public bool halfAtStart;
public string animName;
public Collider leverColl;
public Animator lever;
public float leverSpeed;
public Animator movTarget;
public float movSpeed;
public float moveSpeedMax;
public bool pause;
public Camera rayCam;
public bool reverseLever;
public bool noCutoff;
public bool checkCompletion;
public OutriggerController orControl;
public Button returnBtn;
// Use this for initialization
void Start () {
if(lever != null)
lever.speed = 0;
movTarget.speed = 0;
if (halfAtStart) {
if(lever != null)
lever.Play(0, -1, 0.5f);
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
if(Physics.Raycast(rayCam.ScreenPointToRay( Input.mousePosition ), out hit, Mathf.Infinity)){
//print("GameObject named " + hit.collider.gameObject.name + " | tagged " +hit.collider.gameObject.tag);
//if(hit.collider.gameObject.tag == "Lever"){
if(hit.collider == leverColl){
//print("thisCollider");
//returnBtn.GetComponent<Animator> ().SetTrigger ("Show");
dragging = true;
mouseDownPoint = new Vector2( Input.mousePosition.x / Screen.width , Input.mousePosition.y / Screen.height ) ;
}else{
//Debug.Log("check your rays!!!");
}
}
//if(hit){
//}
}
if (dragging) {
if (Input.GetMouseButton (0)) {
mouseDragPoint = new Vector2 (Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height);
}
// Debug.Log("dragPoint " + mouseDragPoint.y + " |" + " downPoint " + mouseDownPoint.y);
SetAnimSpeed ();
if (Input.GetMouseButtonUp (0)) {
dragging = false;
if (lever != null)
lever.speed = 0;
movTarget.speed = 0;
if (checkCompletion) {
//orControl.CheckComplete();
}
}
} else {
movTarget.speed = 0;
if(halfAtStart){
lever.Play(0, -1, 0.5f);
}else{
lever.Play(0, -1, 0f);
}
}
}
void SetAnimSpeed(){
//print ("setenter");
yDif = 0 + ( mouseDragPoint.y - mouseDownPoint.y);
//Debug.Log ("yDfi = " + yDif);
if (reverseLever) {
yDif = yDif * -1;
}
if (!noCutoff) {
if (yDif > 0 && atEnd == true) {
if (linear) {
yDif = 0;
//print ("setenter2");
if(lever != null)
lever.speed = (yDif * leverSpeed);
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
//returnBtn.GetComponent<Animator>().SetTrigger("Show");
} else {
if(lever != null)
lever.speed = 0;
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
}
} else if (yDif < 0 && atStart == true) {
if (linear) {
yDif = 0;
//print ("setenter3");
if(lever != null)
lever.speed = (yDif * leverSpeed);
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
//returnBtn.GetComponent<Animator>().SetTrigger("Show");
} else {
if(lever != null)
lever.speed = 0;
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
}
} else {
if(lever != null)
//print ("setenter4");
lever.speed = (yDif * leverSpeed);
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
}
} else {
//print ("setenter5");
if(lever != null)
lever.speed = (yDif * leverSpeed);
if(mouseDragPoint.y > mouseDownPoint.y){
movTarget.speed = (-1 * movSpeed);
}else if(mouseDragPoint.y < mouseDownPoint.y){
movTarget.speed = (movSpeed);
}
movTarget.playbackTime = 0;
}
Mathf.Clamp (movTarget.speed, -moveSpeedMax, moveSpeedMax);
}
public void SetEnd(int end){
if (end == 0) {
atStart = true;
atEnd = false;
} else if (end == 1) {
atStart = false;
atEnd = true;
} else {
atStart = false;
atEnd = false;
}
}
public void TrueLoop(string layername){
if (yDif < 0 && !pause) {
pause = true;
movTarget.Play(layername, 0, 4f);
//StartCoroutine ( PauseLoop());
}
}
/*
IEnumerator PauseLoop(){
yield return new WaitForSeconds(1f);
pause = false;
}*/
}
Your answer
Follow this Question
Related Questions
Animator.Play(state) gives error 0 Answers
Cannot use an AnimationClip created from script with PlayableAPI 0 Answers
How to play an animation when a slider is at a certain value. 1 Answer
Load Animator From XML without using UnityEditor 0 Answers
StateMachineBehaviour Issue when swapping runtimeAnimatorController 0 Answers