- Home /
need help with this script
I am trying to move my game object in circular shape but my code is working only x and y axies only, can you help to find the solution
var explode:GameObject[];
explode=GameObject.FindGameObjectsWithTag("Player");
function OnGUI ()
{
if(GUI.Button(Rect(0,0,50,30),"Explode"))
{
for( var i=0;i<explode.Length;i++)
{
for(var j=7;i<j;j--)
{
explode[i].transform.position=Vector3(i,j,-10);
}
}
}
}
i have 8 cubes with tag name Player. when i pressed Explode button all cube are in circular position. please help me
If you want to make them move in a circular shape then why not parent them to a GameObject and simply spin the GameObject on an axis?
Answer by gajdot · Nov 20, 2013 at 09:06 AM
K, tested it and now updated:
#pragma strict
var explode:GameObject[];
function Start () {
}
function Update () {
}
function OnGUI (){
if(GUI.Button(Rect(0,0,50,30),"Explode")){
var PI: float = 3.14;
var size : int = explode.Length;
var theta_scale : float = (2.0 * PI) / size;
var r : float = 5;
var theta : float = 0;
for(var i = 0; i< explode.Length; i++)
{
var x : float = r*Mathf.Cos(theta);
var y : float = r*Mathf.Sin(theta);
theta = theta + theta_scale;
explode[i].transform.position= new Vector3(x, y, 0);
}
}
}
this will place them around the orthogonal 0,0. If you want to modify the center position, just add the center coordinate to the x and y value.
use $$anonymous$$athf.PI instaed of the PI variable or just declare it to 3.14. Try maybe both and check which works best.
I'm a Hungarian from Romania :D but please accept the answer, so others would know you got the answer you where looking for.