- Home /
Defining a local spawn offset
var x = transform.position.x + Random.Range(-10,10);
var z = transform.position.z + Random.Range(-10,10);
var posn = Vector3(x,transform.position.y,z);
How can I write this so that "posn" refers to the local x/z axis of the gameobject which is running the script, as opposed to x/z relative to world space?
I am basically trying to instantiate a series of objects at "posn" which should be a random X and Z offset relative to the game object rather than the world.
I've tried using TransformDirection but I don't seem to be able to incorporate it into this script without a load of errors, perhaps as I am already using a vector3 to define posn?
so simple! it's just
transform.localPosition.x += whatever
check doco localPosition
it compiles fine with transform.localPosition ins$$anonymous$$d of transform.position but it's not changing anything. All of my instantiated objects still appear to be on the world Z/X axis, irrespective of the dynamic rotation of the object that is running the script (in this case a trigger)
i dopn't follow you, but learn about:
Debug.DrawLine
it's fantastic for debugging this sort of thing. You will have the problem fixed in far less time than typing a question here. Hope it helps!
Answer by lodendsg · Sep 06, 2012 at 09:17 AM
Not sure if I fallow correctly but if you wanted a vector local to the gameObject.transform it would be
`posn = posn - transform;` which in your code above would be the same as
post = new Vector3(Random.Range(-10,10), 0, Random.Range(-10,10));
to be honest when applied to my script that's just making all of my instantiated prefabs detatch completely from the parent trigger (they were previously spawning in it's centre) and now they are spawning at world coordinated 0.0.0 along world plane x/z
Your answer
Follow this Question
Related Questions
Converting Raycasted Point to Local Space 2 Answers
Is there a local method for Rigidbody.constraints? 0 Answers
Apply a Rotation in World Space based on Quaternions 2 Answers
Can someone help me understand world/local space? 1 Answer
an ignorant's trigonometry woes: rotate a transform around an axis 2 Answers