Hey guys, how can I spawn gameObjects on a Circle with Circle Collider2D so that the gameObjects also have automatically the right rotation?
Here is an example what I mean:
So the spike should spawn randomly on the circle with the right rotation.
Hey guys, how can I spawn gameObjects on a Circle with Circle Collider2D so that the gameObjects also have automatically the right rotation?
Here is an example what I mean:
So the spike should spawn randomly on the circle with the right rotation.
I am not familiar with 2D games, but here is the idea :
//Drag & drop your circle from the inspector
public CircleCollider2D Circle ;
public void PlaceGameObject( GameObject gameObject )
{
float angle = Random.value * 360 ;
gameObject.transform.position = Circle.gameObject.transform.position + Circle.offset + // Center of the collider
Vector3.right * Circle.radius * Mathf.cos( angle ) + // Horizontal position
Vector3.up * Circle.radius * Mathf.sin( angle ) ; // Vertical position
gameObject.transform.rotation.SetLookRotation( Vector3.forward, gameObject.transform.position - (Circle.gameObject.transform.position + Circle.offset) ); // Try -Vector3.forward if Vector3.forward doesn't work
}