• Contact

Sudeep's Blog – The Programming Adventure

~ code is poetry

Tag Archives: code snippet

Starting Animation from random time frame Unity

11 Tuesday Jul 2017

Posted by Sudeep Acharya in C Sharp, Unity3D

≈ Leave a comment

Tags

code snippet

Code snippet to start the animation at different time. Useful when there are many same animation running on screen and you want to make them random.

Animator anim = GetComponent<Animator> ();
AnimatorStateInfo state = anim.GetCurrentAnimatorStateInfo (0);
anim.Play (state.fullPathHash, -1, Random.Range(0f,1f));

You can add this code in Start or Awake function and add to the GameObject with the Animator component.

Advertisements

C# code to shuffle Array

09 Sunday Jul 2017

Posted by Sudeep Acharya in C Sharp, Unity3D

≈ Leave a comment

Tags

code snippet

private T[] ShuffleArray<T>(T[] array) {
    System.Random r = new System.Random ();
    for(int i = array.Length; i > 0; i--) {
        int j = r.Next (i);
        T k = array [j];
        array [j] = array [i - 1];
        array [i - 1] = k;
    }

    return array;
}

Example:

Int Array

int[] intArray = new int[] {1,2,3,4};

intArray = ShuffleArray(intArray);

Float Array

float[] floatArray = new float[] {1.0f,2.0f,3.0f,4.0f};

floatArray = ShuffleArray(floatArray);

Recent Posts

  • Learn Kotlin – Class
  • Learn Kotlin – Function
  • Learn Kotlin – Control Flow
  • Learn Kotlin – Array, Collection and Range
  • Learn Kotlin – Hello world and Basic Data Types

Categories

AndEngine Android Android App C++ C Sharp Django Functional Programming Game Game Development Github Haskell JavaScript Kotlin Laravel PHP Python Ubuntu Uncategorized Unity3D Windows 8 WordPress

Archives

  • January 2019
  • December 2018
  • December 2017
  • November 2017
  • July 2017
  • May 2017
  • March 2017
  • November 2016
  • October 2015
  • September 2015
  • April 2015
  • January 2015
  • September 2014
  • August 2014
  • June 2014
  • May 2014
  • April 2014
  • February 2014
  • January 2014
  • December 2013

Follow me on Twitter

My Tweets
Advertisements
असतोमा सद्गमय ।
तमसोमा ज्योतिर् गमय ।
मृत्योर्मामृतं गमय ॥
ॐ शान्ति शान्ति शान्तिः ।।

Powered by WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy