'Instaniate objects on Raycast
So, I want the code to instantiate clone objects onto the new Raycast points if the int i<Random.Range
I have a code that allows to position an object onto the other object by Raycast. Here it is:
public bool RCnn;
public bool RCCheck;
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.GetComponent<SelectableTrunk>())
{
RandomSTrunk.position = hit.point;
RandomSTrunk.LookAt(hit.transform.position + hit.transform.forward, hit.normal);
for (int i = 0; i < RandomSTRUNKnumber; i++)
{
Transform clone = Instantiate(RandomSTrunk);
}
RCnn = false;
RCCheck = false;
}else
{
RCnn = true;
RCCheck = true;
}
}
}
Then I am passing RCNN and RCCheck to the other script and make a loop to change raycast point until it hits the place I need to be hit:
Childnumber = childRC1.RCnn;
Childcheck = childRC1.RCCheck;
while (Childnumber == true)
{
if (TrunkScale < 0.75f)
{
RandomMinMax = Random.Range(1.0f, 0.3f);
transform.position = new Vector3(0.0f, RandomMinMax, 0.0f);
}
if (Childcheck == false)
{
continue;
}
if (Childcheck == true)
{
break;
}
}
I cannot understand the way to clone the object for example 3 times and place them into 3 new Raycast hit points after checking if those points are hit. And after that remove previous positions and do everything again by pressing a button.
In my opinion, there is some stuff in the "for" loop, but I do not know where to implement it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
