Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation looping on C# Boolean Value

Discussion in 'Animation' started by Geeknerd1337, Jan 17, 2015.

  1. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    52
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LadderControl : MonoBehaviour {
    5.    
    6.     public bool LadderCont;
    7.     public Collider LadderMesh;
    8.     public MeshRenderer LAC;
    9.     private bool Anim;
    10.    
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.    
    20.        
    21.        
    22.         if(LadderCont == false){
    23.             LAC.animation["LadderUp"].wrapMode = WrapMode.Once;
    24.             LAC.animation.Play("LadderUp");
    25.             LadderMesh.enabled = false;
    26.            
    27.            
    28.            
    29.     }else{
    30.             LAC.animation.Play("LadderDown");
    31.             LadderMesh.enabled = true;
    32.         }
    33. }
    34. }
    35.  
    Basically, I have a button activated using a Raycast. I have an animation for a ladder coming up and a ladder sliding back down. However, the animation loops for some reason. Can anyone help me with this small problem?
     
  2. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    52
    Please, I could really use the help.
     
  3. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    With your logic, it looks like EITHER Play(ladderup), OR Play(ladder down) will be called every single update. You probably want to call this function inside some kind of onclick logic, rather than right inside update like that. Or perhaps just add some change detection, for LadderCont.

    simple change detection example:
    Code (CSharp):
    1.  
    2.     public bool LadderCont;
    3.     private bool lastLadderCont;
    4.     void Update () {
    5.         if(lastLadderCont!=LadderCont)
    6.         {
    7.             lastLadderCont=LadderCont;
    8.             if(LadderCont == false){
    9.             //....play
    10.             }else{
    11.             //....play
    12.             }
    13.         }
    14.     }
     
    Last edited: Jan 19, 2015