- Home /
Random Prefab around Sphere convert C# to Javascript
Hi mates,
I got a small C# snippet what i would like to convert to Js.
What is the script doing? It instantiate for example 10 Cubes randomly around a sphere.
I started already to convert it to Java, but still got lots of errors. There must be no context menu, it could work also just on Start. Would be nice if someone is able to help me. I already was looking for converter tools.
Here the C#:
using UnityEngine;
using System.Collections.Generic;
public class PointsOnSphere : MonoBehaviour {
public GameObject prefab;
public int count = 10;
public float size = 20;
[ContextMenu("Create Points")]
void Create () {
var points = UniformPointsOnSphere(count, size);
for(var i=0; i<count; i++) {
var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity) as GameObject;
g.transform.parent = transform;
}
}
Vector3[] UniformPointsOnSphere(float N, float scale) {
var points = new List<Vector3>();
var i = Mathf.PI * (3 - Mathf.Sqrt(5));
var o = 2 / N;
for(var k=0; k<N; k++) {
var y = k * o - 1 + (o / 2);
var r = Mathf.Sqrt(1 - y*y);
var phi = k * i;
points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
}
return points.ToArray();
}
}
Here my Java so far.
var prefab : GameObject;
var count : int = 10;
var size : float = 20;
var points : Array;
function Start () {
var points = UniformPointsOnSphere(count, size);
for(var i=0; i<count; i++) {
var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity);
g.transform.parent = transform;
}
}
function UniformPointsOnSphere ( N : float , scale : float ) : Vector3[] {
var points = new Array (Vector3(0, 0, 0));
var i = Mathf.PI * (3 - Mathf.Sqrt(5));
var o = 2 / N;
for(var k=0; k<N; k++) {
var y = k * o - 1 + (o / 2);
var r = Mathf.Sqrt(1 - y*y);
var phi = k * i;
points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
}
return points.Array;
}
Java is an actual language that is very different than javascript. They are unfortunately named.
And this gives an error? Looks fine to me, just cast all your variables. What errors are you getting?
Yeah i mean javascript.
Get this Error now. "$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityScript.Lang.Array.ToArray'.
Well "points" is an array, so you don't need to call "ToArray()".
I did updated the code above. Hope "return points.Array;" is right.
Now i got this error.
NullReferenceException: Object reference not set to an instance of an object
@this line "var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity);"
So must be +points[i] ....
Does anyone see something what is really wrong? Im not a C# expert :=)
Answer by crimi · Jan 22, 2013 at 09:41 PM
I did giving up to convert the script and started from scratch, so here is my solution. I also wanted to face the cube the center.
var thePrefab : GameObject;
var count : int = 10; // Number of cubes
var sphereSize : float = 10; // Radius of sphere
function Start () {
var center = transform.position;
for (var i=0; i < count; ++i) {
yield WaitForSeconds (0.5); // Spawning delay
var pos = Random.onUnitSphere * sphereSize;
var rot = Quaternion.FromToRotation(Vector3.forward, center-pos); // Face cubes to center
Instantiate(thePrefab, pos, rot);
}
}
Your answer
Follow this Question
Related Questions
Randomly Instantiating an object inside a shrinking sphere? 1 Answer
Instantiate a Prefab along the surface of another GameObject (Sphere) 1 Answer
Moving rigidbodies at random 1 Answer
Problems with an object rotating a sphere surface (Quaternions and LookAt) 0 Answers
[C#] Algorithm for randomly packing random radius sphere in fixed radius sphere. 0 Answers