Saad Khawaja Menu

Snippets

Permalink:

Play a specific frame of Animation (Legacy)

Unity does not have a method to goto a specific frame per say. But you can move to a specific time interval of an animation using Gameobject.animation["name"].time – Legacy Animation

So in short, you can actually goto a specific frame by finding the time of the animation at that particular frame. e.g. If the animation speed (visible in the animation window) is 24 fps: It means 24 frames are rendered in 1 sec.

The first frame will be at 1/24 * 1. The second will be at 1/24 * 2. and so on.

So this snippet will do the trick:

gameobjectname.animation.Play ("Take 001");
gameobjectname.animation.Stop ();
gameobjectname.animation["Take 001"].speed = 0; //to make the animation pause
gameobjectname.animation["Take 001"].enabled = true;
gameobjectname.animation["Take 001"].time = (1f/24f)*frameNumber;
  • Smithc669

    I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again! aeddcaddgbgdaeef

  • martin

    i was looking at this for my own use and im sure it will work however im not sure how to initialise frameNumber and the code is deprecated from unity 5 trying to swap this for GetComponent works fine for .Play but not the rest it seems any ideas?

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.

Permalink:

  • sai

    But how to screenshots with batch mode?

  • Helda Bbt

    Peut ton récupérer les captures faire dans un groupe d’objets genre Texture2D []??

  • •••
  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.

Permalink:

Get Time from the Server

A common error that developers make is that they use the time on the device for various game play elements.

For e.g. A game that requires the user to wait 2 hours before making another element.

This can be easily hacked by changing the device date/time. So, in order to stop cheating, you must get time from the server and compare the current time with that.

 

The easiest solution would be to make a PHP page and print time in it.

Then use WWW in Unity to query the page and get time.

 

PHP

  1. <?php
  2. print date(“G.i:s”, time());
  3. ?>

UNITY

void Start () {
        StartCoroutine("getTime");
 
    }
 
    IEnumerator getTime()
    {
        WWW www = new WWW("http://www.saadkhawaja.com/gettime.php");
        yield return www;
 
        Debug.Log("Time on the server is now: " + www.text);
    }
  • nobody

    hi, can you make the php include the date also ?
    it mean so much for me,
    thank you

  • asd

    hi, im using this solution… btw i’ve made my script to load on my server with apache, but unfortunately it require a user\pass to get the page load… if I put my page in CS script i got 401 error authentication required… do you know anyway to bypass that?

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.

Permalink:

Move Object with Mouse

Move a 3D Object with Mouse – and make it collide

  • Melvin

    Awesome just what I needed! One question though, I’m doing this for a 2D Air Hockey game. I got the code to work, but when my paddle goes real quickly against the puck, then it goes through the puck. I tried this code in both FixedUpdate() and Update(). This is probably the case, because the move happens so quick, that the Unity engine does not check for collision in that fast mouse movement. I’m currently thinking of ways how to fix this, but perhaps you have a good idea for this?

  • qwertyuiopasdfghjklzxcvbnm

    on your rigidbody, change collision detection to continuous instead of discrete

  • Thanks for leaving a comment, please keep it clean. HTML allowed is strong, code and a href.