- Home /
Question by
Cuckboii69 · Jun 13, 2021 at 09:07 PM ·
c#movementvector3movement scriptvector2
Get a position relative to a gameobject
Hello, trying to get a random position relative to a gameobject. As in how would I modify the following code to do this, I understand how this uses the whole gameworld to get a coordinate, however how would I make it so that the gameobject is essentially the 0,0,0 coordinate
private Vector2 target;
void Start()
{
target = new Vector3(Random.Range(-69.0f, 100.0f), Random.Range(-420.0f, 100.0f), Random.Range(-420.0f, 100.0f));
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, 500f * Time.deltaTime);
}
Comment
I am a bit confused, are you using 2D or 3D math? Because you are mixing Vector2 and Vector3 a lot (there's an implicit conversion between the two, you are losing the z value)
Add it to the gameObject's position
Vector3 relativePos = thatGameObject.transform.position + randomGeneratedPosition;