User talk:Lzt120

From Unify Community Wiki
Jump to: navigation, search

Random Movement of in 2D

This script is modified from one script on UnityAnswer and post here for one may need.

#pragma strict
enum Type2D {XY,XZ,YZ}
 
var Type2D : Type2D[];
 
var yourMovementObject : Transform[];
var randomRate: float[];
var moveSpeed:float[];
 
class MaxPosition
{
    var UpMaxPositionX : float= 5.0;
    var DownMaxPositionX : float= -5.0;
 
    var UpMaxPositionY : float = 5.0;
    var DownMaxPositionY : float = -5.0;
 
    var UpMaxPositionZ : float  = 5.0;
    var DownMaxPositionZ : float = -5.0;
}
 
var MaxPosition : MaxPosition[];
private var RandomX : int;
private var RandomY : int;
private var RandomZ : int;
 
function Update ()
{
 
//////////////////////////////////////////////x,y,z/////////////////////////////////////////////////////////////////////////////
 
   for(var cnt : int = 0;cnt < yourMovementObject.Length;cnt++)
   {
	   	Invoke("RandomlyMove",randomRate[cnt]);
	   	switch (Type2D[cnt])
	   {
	        case Type2D[cnt].XY:
	            yourMovementObject[cnt].Translate(Vector3(RandomX,RandomY,0) * moveSpeed[cnt] * Time.deltaTime);
	            break;
	        case Type2D[cnt].XZ:
	            yourMovementObject[cnt].Translate(Vector3(RandomX,0,RandomZ) * moveSpeed[cnt] * Time.deltaTime,Space.World);
	            break;
	        case Type2D[cnt].YZ:
	            yourMovementObject[cnt].Translate(Vector3(0,RandomY,RandomZ) * moveSpeed[cnt] * Time.deltaTime,Space.World);
	            break;
	    }
 
	    if (yourMovementObject[cnt].transform.position.x > MaxPosition[cnt].UpMaxPositionX)
	        yourMovementObject[cnt].transform.position.x = MaxPosition[cnt].UpMaxPositionX;
	    else if (yourMovementObject[cnt].transform.position.x < MaxPosition[cnt].DownMaxPositionX)
	        yourMovementObject[cnt].transform.position.x = MaxPosition[cnt].DownMaxPositionX;
 
	    if (yourMovementObject[cnt].transform.position.y > MaxPosition[cnt].UpMaxPositionY)
	        yourMovementObject[cnt].transform.position.y = MaxPosition[cnt].UpMaxPositionY;
	    else if (yourMovementObject[cnt].transform.position.y < MaxPosition[cnt].DownMaxPositionY)
	        yourMovementObject[cnt].transform.position.y = MaxPosition[cnt].DownMaxPositionY;
 
	    if (yourMovementObject[cnt].transform.position.z > MaxPosition[cnt].UpMaxPositionZ)
	        yourMovementObject[cnt].transform.position.z = MaxPosition[cnt].UpMaxPositionZ;
	    else if (yourMovementObject[cnt].transform.position.z < MaxPosition[cnt].DownMaxPositionZ)
	        yourMovementObject[cnt].transform.position.z = MaxPosition[cnt].DownMaxPositionZ;
   }      
}
 
function RandomlyMove ()
{
    RandomX = Random.Range(-2,3);
    RandomY = Random.Range(-2,3);
    RandomZ = Random.Range(-2,3);
    CancelInvoke("RandomlyMove");
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox