Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Start animation at specific frame not working

Discussion in 'Animation' started by Washburn, May 29, 2017.

  1. Washburn

    Washburn

    Joined:
    Oct 13, 2015
    Posts:
    21
    Hi,
    I created a gameObject, added animations etc all working fine.
    I actually create 6 of the same object.
    All the animatins start at the same time.
    I want to mix it up a little by starting each at random frames within the animation.
    I have read the docs and questions/answers on the forum relating to this exact issue and tried many of them, none of them seem to work.
    I have a trigger that calls a spawn manager to instanatiate the game object.
    I have a script on the gameobject that should randomize the start frame.
    I can see in the console random numbers being generated but the frames are the same when the animation plays.
    Any help appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomStartFrame : MonoBehaviour {
    6.  
    7.     Animator anim;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.         anim = GetComponent<Animator>();    
    13.         {
    14.             int pickAnumber = Random.Range(1, 15);
    15.             anim.Play("Idle1", -1, pickAnumber);          
    16.             Debug.Log(pickAnumber);
    17.         }
    18.     }
    19. }
    20.  
     
  2. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    try

    Code (CSharp):
    1. float totalFrames = 100; //total frames of your animation
    2. anim.Play("Idle1", -1, (1f/totalframes)*pickAnumber)
     
  3. Bryan77

    Bryan77

    Joined:
    Jun 9, 2014
    Posts:
    24
    Hello. I am trying to do the same thing, and I keep getting the error

    "error CS1501: No overload for method 'Play' takes 3 arguments"
     
  4. Merrick_Oost

    Merrick_Oost

    Joined:
    Feb 11, 2020
    Posts:
    1
    If you want to start it at any random place in the animation:
    Code (CSharp):
    1.   void Start()
    2.     {
    3.         GetComponent<Animator>().Play("SpikeBall", -1, Random.Range(0f, 1f));
    4.     }