Warning: Creating default object from empty value in /home2/nasim55/public_html/saadkhawaja.com/wp-content/plugins/contact-form-7/includes/controller.php on line 131
Snippets for Unity3D - Learn Unity - Saad Khawaja
Category

Snippets

Play a specific frame of Animation (Legacy)

By | Snippets | 2 Comments

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;

Get Time from the Server

By | Snippets, Uncategorized | 3 Comments

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);
    }

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

By | Snippets | 2 Comments

A basic problem that many Unity developers face is that they use Transform.Translate or update Transform.position of an object and expect it to collide with other objects.

This is not the case. When you’re using Transform.Translate or Transform.Position, you’re essentially just teleporting the object to that position. Hence, there will be no collision with intersecting objects.

Fortunately, Unity has provided a function rigidbody.MovePosition which takes care of this. The snippet below moves a 3D object (on which it is attached) with the mouse and collides with all objects that come in its way.


using UnityEngine;
using System.Collections;

public class MoveWithMouse : MonoBehaviour {



	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	

		Vector3 v3 = Input.mousePosition;
		v3.z = 10.0f;
		v3 = Camera.main.ScreenToWorldPoint(v3);

		Debug.Log(v3); //Current Position of mouse in world space

		this.gameObject.rigidbody.MovePosition(v3);

	}



}