- Home /
how to shoot bullets from circumference of a circle ?
[1]: /storage/temp/128186-gamepic.png
hello guys! i have attatched an image below a project i am working on. When i click anywhere on the screen the blue ball below will shoot a small ball in that direction from the center but i want to shoot it from the circumference of the circle in center.How do i do it?
Answer by Defetysta · Nov 22, 2018 at 03:29 PM
I'd try to store the radius of the circle, determine the vector from middle of the circle to the place you clicked, normalize it and then multiply the normalized vector times range, like
float radius = 2f;
Vector2 direction = (circle.transform.position - clickPosition).normalized;
Vector2 placeToSpawn = direction * radius;
Can't guarantee it works, I don't have access to a compiler right now, but it should be something along those lines.
Thanks man this will work but actually the radius of circle changes in game,how do i get that radius?
The following code should do:
private Renderer rend;
private Vector3 center;
private float radius;
private void Start()
{
rend = GetComponent<Renderer>();
}
private void Update()
{
center = rend.bounds.center;
radius = rend.bounds.extents.magnitude;
}
The code will be executed each and every frame, you may want to do it only when the size of the circle changes (in which case Events will be useful) or just for example every 10th frame.
hey man it actually didn't work.It spawns only in one direction.
here black is the center part and blue ones are bullets.
Are you sure you are multiplying the spawn point by the direction vector?
Vector2 direction = (circle.transform.position - clickPosition).normalized;
Attach your code please.