Friday, 17 August 2012

Create plane randomly in Unity 3D


step 1: take 3 plane
step 2:add cube and attach it to plane as a parent and set the tag name to them
step 3:add one cube to move and attach rigidbody
step 4:disp_pos=start_pos-initial_pos

step 5 :Script:-
using UnityEngine;
using System.Collections;

public class testproject : MonoBehaviour {

float _movespeed=5.0f;
private int _my_rand_val;

private Vector3 initial_pos =new Vector3(0f,0.904f,-10.03f);

private Vector3 start_pos =new Vector3(0f,1f,6.42f);

private Vector3 disp_pos =new Vector3(0f,0.1f,16.45f);

private Vector3 storeval;
public GameObject green;
public GameObject pink;
public GameObject blue;



// Use this for initialization
void Start () {

storeval=start_pos+disp_pos;

}

// Update is called once per frame
void Update () {

float _moveforward;
float _moveright;

_moveforward=Input.GetAxis("Vertical")*_movespeed*Time.deltaTime;
_moveright=Input.GetAxis("Horizontal")*_movespeed*Time.deltaTime;

transform.Translate(Vector3.forward*_moveforward);
transform.Translate(Vector3.right*_moveright);



}

void OnTriggerEnter(Collider other)
{
if(other.tag == "cube_collider")
{
print ("fine");
_rand_func();
Destroy(other.transform.parent.gameObject);


}
}


void _rand_func()
{
_my_rand_val=Random.Range(0, 3);
storeval=storeval+disp_pos;

if(_my_rand_val == 0)
{
Instantiate(green,storeval,Quaternion.identity);
}
if(_my_rand_val == 1)
{
Instantiate(blue,storeval,Quaternion.identity);

}
if(_my_rand_val == 2)
{
Instantiate(pink,storeval,Quaternion.identity);
}
}

}

       

No comments:

Post a Comment