Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TetchedOne · Jul 23, 2015 at 10:09 PM · scripting problemanimatorplayback

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;
     }*/
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges