- Home /
Calcultate spawn position based on spawn volume's rotation
I have created a empty GameObject and added a BoxCollider to it. I'm using the size of the collider to define a spawning volume for drones in the game. It works nice, but it doesn't take into account the rotation for the GameObject/BoxCollider. So I need some sort of fancy math equation here to calculate the spawn position for the drones based on the spawn volumes rotation in the scene.
Here is the code so far:
using UnityEngine;
using System.Collections;
public class SpawnArea : MonoBehaviour
{
public GameObject Drone;
public float TimeLeft;
public float MinTime = 1.0f;
public float MaxTime = 2.0f;
void Start ()
{
TimeLeft = Random.Range(MinTime, MaxTime);
}
void Update ()
{
TimeLeft -= Time.deltaTime;
if (TimeLeft < 0)
{
BoxCollider volume = GetComponent<BoxCollider>();
Vector3 size = volume.size;
Vector3 offset = volume.center;
Vector3 pos;
pos.x = Random.Range(transform.position.x + offset.x - size.x * transform.localScale.x / 2, transform.position.x + offset.x + size.x * transform.localScale.x / 2);
pos.y = Random.Range(transform.position.y + offset.y - size.y * transform.localScale.y / 2, transform.position.y + offset.y + size.y * transform.localScale.y / 2);
pos.z = Random.Range(transform.position.z + offset.z - size.z * transform.localScale.z / 2, transform.position.z + offset.z + size.z * transform.localScale.z / 2);
Quaternion rot;
rot = Quaternion.Euler(transform.rotation.eulerAngles.x - 180, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z - 90);
Instantiate(Drone, pos, rot);
TimeLeft = Random.Range(MinTime, MaxTime);
}
}
}
Any ideas on how to fix this? :)
Answer by KristofferH · Apr 03, 2016 at 08:38 PM
I found a solution to this problem in another thread. Here is the new code:
using UnityEngine;
using System.Collections;
public class SpawnArea : MonoBehaviour
{
public GameObject Drone;
public float TimeLeft;
public float MinTime = 1.0f;
public float MaxTime = 2.0f;
void Start ()
{
TimeLeft = Random.Range(MinTime, MaxTime);
}
void Update ()
{
TimeLeft -= Time.deltaTime;
if (TimeLeft < 0)
{
BoxCollider volume = GetComponent<BoxCollider>();
Vector3 size = volume.size;
Vector3 offset = volume.center;
Vector3 pos;
pos.x = Random.Range(transform.localPosition.x + offset.x - size.x * transform.localScale.x / 2, transform.localPosition.x + offset.x + size.x * transform.localScale.x / 2);
pos.y = Random.Range(transform.localPosition.y + offset.y - size.y * transform.localScale.y / 2, transform.localPosition.y + offset.y + size.y * transform.localScale.y / 2);
pos.z = Random.Range(transform.localPosition.z + offset.z - size.z * transform.localScale.z / 2, transform.localPosition.z + offset.z + size.z * transform.localScale.z / 2);
pos = RotatePos(pos, transform.position, transform.rotation.eulerAngles);
Quaternion rot;
rot = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
Instantiate(Drone, pos, rot);
TimeLeft = Random.Range(MinTime, MaxTime);
}
}
public Vector3 RotatePos(Vector3 point, Vector3 pivot, Vector3 angles)
{
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
}
And here is the link to the original thread: http://answers.unity3d.com/questions/532297/rotate-a-vector-around-a-certain-point.html
Answer by phxvyper · Mar 31, 2016 at 07:51 PM
Theres a fancy little function that does all that work for ya:
Vector3 worldPos = transform.TransformPoint(pos);
Instantiate with worldPos instead of pos. Heres the documentation for this function.
Ah, well yes that did take care of the rotation problem, but the positions are now way of ins$$anonymous$$d. So the drones seems to be spawning within the spawn volume but it is like the spawn volume itself is located in a different area. So there seems to be some problem with my calculation of the variable pos.
@$$anonymous$$ristofferH were you adding the new position to the position of your volume? If you were, this is why it seemed off.
Do TransformPoint on your relative position and treat that as a world position. Dont add it to anything and it will work