- Home /
Random instantiation at an object
Hello,
As the title suggests, I'm trying to instantiate an object around a particular object (tree) and within a random range around the area have them spawn. As I don't want to object to appear at the same point every time.
I looked on the unity scripting reference at "Random" but not sure if I need to do something else to implement this properly.
function TreeHit (treeHit : int)
{
treeHealth -= treeHit;
if(treeHealth <= 25)
{
var position : Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0));
Instantiate(apple, tree.transform.position, Quaternion.identity);
treeHealth = 100;
}
}
Answer by MasterTim · Oct 27, 2013 at 08:43 PM
function TreeHit (treeHit : int){ treeHealth -= treeHit; if(treeHealth <= 25) { var position : Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0)); Instantiate(apple, tree.transform.position+positiom, Quaternion.identity); treeHealth = 100; }}
I see! I didn't realise I could use "position" again. I now realise it's the position in the transform is linked to the statement. Thanks for that, cleared up my issue.
In the future, @$$anonymous$$asterTim you should try and post your code on more than one line, if it is actually more than one line. Looking at code like this is difficult and slow.
Your answer
Follow this Question
Related Questions
'text' is not a member of 'OBJECT' JavaScript Not Working in Unity 3.5 - Lost Commands? 2 Answers
Touch Detection on Object in javascript 0 Answers
Controlling two object with one Javascript 2 Answers
Move object back automatically 1 Answer
Attaching a script to a second object ruins everything 1 Answer