- Home /
working with RotateArround
I have one cube in some point (x,y,z);
I would like to put Sphere -s into its nodes.
This script I connect with Cube.
public class HandsOnCube : MonoBehaviour {
// Use this for initialization
private GameObject[] PNodes;
private Vector3[] pos;
private Vector3 offset;
private Quaternion offrot;
void Start ()
{
offrot = transform.rotation;
offset = transform.position - transform.localScale / 2;
pos = new[] {
new Vector3 (0f, 0f, 0f),
new Vector3 (0f, 0f, 1f),
new Vector3 (0f, 1f, 0f),
new Vector3 (0f, 1f, 1f),
new Vector3 (1f, 0f, 0f),
new Vector3 (1f, 0f, 1f),
new Vector3 (1f, 1f, 0f),
new Vector3 (1f, 1f, 1f)
};
int[] inds = new [] {0,1,2,3,4,5,6,7};
PNodes = new GameObject[8];
foreach (int i in inds) {
PNodes [i] = new GameObject ();
PNodes [i] = GameObject.CreatePrimitive (PrimitiveType.Sphere);
PNodes [i].transform.position = pos [i] + offset + offset_def;
PNodes [i].transform.localScale = new Vector3 (0.2f, 0.2f, 0.2f);
//PNodes [i].transform.RotateAround = offrot;
RotateGameObjArround (PNodes [i], transform.position, offrot);
PNodes [i].SetActive (true);
}
}
void RotateGameObjArround (GameObject o, Vector3 aP, Quaternion Q)
{ // ap - ArroundPoint
Vector3 angles = Q.eulerAngles;
o.transform.RotateAround (aP, new Vector3 (1f, 0, 0), angles.x);
o.transform.RotateAround (aP, new Vector3 (0, 1f, 0), angles.y);
o.transform.RotateAround (aP, new Vector3 (0, 0, 1f), angles.z);
}
// Update is called once per frame
void Update ()
{}
}
Poblem: if Cube rotation = (10,20,30) then Spheres are not situated in right positions. I think that I have to modify RotateGameObjArround fn, but how? Any ideas?
Answer by Ericool · Jan 09, 2015 at 01:31 PM
The axis are wrong the axis must be local and it has to be a normal , you can get it by doing cross product .
What do you mean in code? I think that Cross((1,0,0),(0,1,0)) = |a| (0,0,1).
Answer by _dns_ · Jan 09, 2015 at 02:57 PM
Hi, if I understand well: you want to create objects at the positions of the vertices of the cube.
The Transform component can transform positions or rotations from the object's space to the world space. In your case: the pos[] table represent the local positions of the vertices of the cube. But in world space, those local position has other coordinates that depends on where the cube is positioned + it's scale & rotation.
Transform.TransformPoint can transform those local coordinates to world coordinates: it will use the position + rotation + scale of the transform component to do so. Here, just use
positionOfTheSphere = transform.TransformPoint( pos[i] );
Then you can setup the scale and rotation of the sphere to anything you want.
I don't understand what you mean by "global transform", could you please give more information ?