I just recently learned how to make games with unity, usually, I just find my answers in forums and youtube videos but this time I really can’t find anything about this problem.
Here’s what I’m trying to do.
I want my object to only spawn in the green zone and not in the red zone or beyond the green zone.
my code currently allows me to spawn my object inside the green and the red zone.
This problem is hard in Cartesian (x/y) coordinates, but easy in polar (angle/radius) coordinates. So, use Random.Range(0, Mathf.PI*2) to pick an angle (in radians), and Random.Range(innerRadius, outerRadius) to pick a radius. Then just use sine and cosine to convert these to x/y coordinates. (x = Mathf.Cos(ang)*r, y = Mathf.Sin(ang)*r.)
(And you thought you’d never use that high-school math! )
I understand the logic behind your response. I feel like it will really work. My problem is that I have no idea how to code that. I would really appreciate it if I get help from anyone with that. For now, I’m gonna try and search it up. I may get an answer somewhere. I will reply to this thread again with the code I made. Thanks a lot
I doubt that searching will turn up more than I gave you. But I gave you all the tricky bits. And you already showed you know how to use Random.Range in your code. So I’m not certain where the confusion lies.
I think you should just roll up your sleeves and give it a try. And then, please do post here the code you come up with. If it’s right, we’ll give you well-deserved congrats; if not, we’ll give you kudos for trying and be able to help with whatever the point of confusion turns out to be.
You’re right. I could not find any tutorial or thread about it at all. I’m really new at this, about a month I think, that’s why there are still lots of things that I do not understand. I think I am very close to solving this one. I’ll be posting my code later Thanks a lot
PS: I am confused about how to use this (x = Mathf.Cos(ang)*r, y = Mathf.Sin(ang)*r.)
It worked! Thank you!
But now I have a different problem.
My previous code allowed me to spawn the objects from the blue circle(which constantly moves around).
My new code only allows the objects to spawn from the center of the screen.
When the blue circle moves the spawn area stays the same.
public class EvolutionEssenceSpawner : MonoBehaviour
{
public GameObject EvolutionEssence;
Vector2 CellLoc;
float randRad;
float randDist;
// Start is called before the first frame update
void Start()
{
CellLoc = GameObject.FindGameObjectWithTag("CellBubble").transform.position;
}
// Update is called once per frame
void Update()
{
randRad = Random.Range(0, Mathf.PI * 2);
randDist = Random.Range(6, 10);
transform.position = new Vector2(Mathf.Cos(randRad),Mathf.Sin(randRad))*randDist;
SpawnEvolutionEssense();
//transform.position = new Vector2(Random.Range(CellLoc.x-3,CellLoc.x+3),Random.Range(CellLoc.y-3,CellLoc.y+3));
}
void SpawnEvolutionEssense()
{
int rand = Random.Range(1, 2);
if(rand == 1)
{
Instantiate(EvolutionEssence, transform.position, Quaternion.identity);
}
}
}
Neat. I never knew this existed but now that I’m looking at it I love how there is one for spheres and even one for placing stuff on the surface of a sphere.
Thank you, friends, for the help. I finally got it to work. I learned what and how to use polar coordinates and “Random.insideUnitCircle” in this thread.
I got an error when I tried Random.insideUnitCircle but I found an answer here.
Here’s my final code.
I tried to use the variable I made “CellLoc” but it does not seem to work so I just used the raw code(dunno how it is called) instead which is “GameObject.FindGameObjectWithTag(“CellBubble”).transform.position”. I don’t understand why is it not working if I use a variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EvolutionEssenceSpawner : MonoBehaviour
{
public GameObject EvolutionEssence;
//Vector3 CellLoc;
Vector3 RandRange;
// Start is called before the first frame update
void Start()
{
//CellLoc = GameObject.FindGameObjectWithTag("CellBubble").transform.position;
}
// Update is called once per frame
void Update()
{
if(SceneManager.GetActiveScene().name != "StartMenu")
{
SpawnEvolutionEssense();
RandRange = (Vector3)Random.insideUnitCircle.normalized * Random.Range(4, 4);
transform.position = GameObject.FindGameObjectWithTag("CellBubble").transform.position + RandRange;
}
}
void SpawnEvolutionEssense()
{
int rand = Random.Range(1, 2);
if(rand == 1)
{
Instantiate(EvolutionEssence, transform.position, Quaternion.identity);
}
}
}