Terrain Clicking Creation in More Detail w/ Video

May 4, 2010 at 6:47 am (game development, Unity3D)

Here is what I learned about Instantiate yesterday:

  1. You can use a prefab, think of it like a stamp, and use it with the Instantiate function to create objects.
  2. The script needs a reference to the prefab.  Right now all I know it that means selecting the prefab in the IDE from exposed object variable in my script.  (This will need to be fixed because I need to be able to set this in code as players pick different objects to place.)

Number 2 is the thing I was stuck on that made my code not work for awhile.  So yeah pretty much you can blame me not knowing what I am doing yet.

Here is the code I have on the terrain to create an object.  I am thinking this will eventually not be on the terrain because what I really want is a copy of the prefab following the mouse till you click to place it.  The Terrain click would send an event to that prefab to place on terrain where it is at.

var SampleObject:GameObject;

function OnMouseUp(){
 var pos = Camera.main.GetComponent("TerrainLine");
 var SampleObject =
     Instantiate(SampleObject,
     Vector3(pos.x_pos, pos.y_pos, pos.z_pos),
     Quaternion.identity);
}

The Camera.main.GetComponent is used to access the script in the camera that is tracking the location on the terrain.  (This is the same script making the terrain highlight.)

Quaternion.identity is completely and shamelessly stolen from a sample script and I am not sure what it means yet.

Here is a video of what the click to place does right now and some basic panning using the arrow or WASD keys.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.