- Home /
Rotate Spawnpoint running into errors
I dont know why coding this is so difficult! i feel dumb!
I want to rotate the spawnpoint inside a loop so that it instantiates 10 gamobjects in different directions. Am i mixing up quaternions and eulerangles? (i tried so many versions of the "spawnpoint.transform.Rotate" that by now it probably has a $yntax error.
var gridX = 1;
var gridY = 8;
var spacing = 1.0;
var blocks : GameObject[];
private var rot : Quaternion;
var sp : GameObject;
sp = GameObject.Find("spawnpoint");
function Start () {
for (var y = 0; y < gridY; y++) {
sp.transform.RotateAround(Vector3.up,20*y);
for (var x = 0; x < gridX; x++) {
rot.eulerAngles = Vector3(0,0,0);//you can rotate this at 45 etc and also mult byx
var ino = Instantiate(blocks[Random.Range(0, blocks.Length)], Vector3 (x-4, 6, 4) * spacing, rot);
ino.transform.localScale += Vector3((Mathf.Pow(Random.Range(0,1.7), 4)),(Mathf.Pow(Random.Range(0,1.6), 5)),(Mathf.Pow(Random.Range(0,1.7), 4)));
}
}
}
EDIT= Apparent Answer:
the instantiation command doesnt seem to have any relation to the spawnpoint position and rotation unless you manually calculate the new global positions and rotations as functions of the spawnpoint rotation and position. ??? i thought that if you just rotate a spawnpoint you can have objects instantiate in positions relative to it, but no!!!
you have to do something like this:..? http://answers.unity3d.com/questions/41726/instantiating-in-a-position-relative-to-an-object.html
Answer by roamcel · Aug 22, 2011 at 12:36 PM
I hope I'm not underestimating your problem, but I think that you just need
spawnpoint.transform.RotateAround(Vector3.up,myangle);
and be absolutely sure that
objRot = spawnpoint.transform.rotation;
since as of now you're setting all of ino's rotations to 0 eulers.
thanks! i wishing to rotate instantiated objects about themselves and also to rotate the spawnpoint so that i can project instantiated objects into patterns with a central symmetry.
so i did the script from zero again, and if i write, as you suggest:
var sp : GameObject;
sp = GameObject.Find("spawnpoint");
function Start () { for (var y = 0; y < gridY; y++) {
sp.transform.RotateAround(Vector3.up,20*y);
.......
i have this error: NullReferenceException: Object reference not set to an instance of an object UnityEditor.ListViewShared.Has$$anonymous$$ouseDown (UnityEditor.InternalListViewState ilvState, Rect r, Int32 button) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$ono/GUI/ListViewShared.cs:180) UnityEditor.ListViewShared.Has$$anonymous$$ouseDown (UnityEditor.InternalListViewState ilvState, Rect r) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$ono/GUI/ListViewShared.cs:175) UnityEditor.ListViewShared+ListViewElementsEnumerator.$$anonymous$$oveNext () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$ono/GUI/ListViewShared.cs:368) UnityEditor.ConsoleWindow.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$ono/ConsoleWindow.cs:422) System.Reflection.$$anonymous$$ono$$anonymous$$ethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
It seems to be a referenced script in a unity system file, maybe because i reinstalled 3.3 after uninstalling 3.4?
Well the downgrade thing might not be of help, however:
assure that
sp = GameObject.Find("spawnpoint");
is not returning null, before sp.transform
besides, I don't remember if you can call RotateAround as a procedure. In C# you need to declare a new transform, copy sp.transform into it, rota$$anonymous$$round the new transform and finally assign sp.transform = newtransform.
Thanks! I found that my main problem was that the instantiation transform.position was not set to the spawnpoint position, omly a globalscale thing, so i obviously have to set the rotation to spawnpoint.transform.rotation+instantiated object rotation, the script was just in the gameobject's attributes but i forgot to assign it!
also there is a bug in some scripts causing an error when i add the line, something to do with keyboard rotate. maybe due to converting a project to 3.4 and back to 3.3, because now my keyboard orbit script is just making things fall in y direction very fast, even though the script is the same. Thanks!!!
Your answer
Follow this Question
Related Questions
Find trajectory angle needed to hit an object 0 Answers
Rotation of an object 3 Answers
Quaternions- how to leave out Y rotations... 1 Answer
Rotation not working 3 Answers
Inheritance code acts differently (Quaternion.LookRotation) 0 Answers