Friday, November 2, 2012

Unity Prefab for Noob

Prefab is something you can add into your scene using the script, such as fireball or bullet

Step to create and use the Unity Prefab
1. Open Unity, under Asset -> Create -> Prefab
2. Drag your game object(fireball) into the new prefab you just created(under project tab)
3. Create a new Javascript


var YourPrefab: Transform;


function Update()
{
   if(Input.GetButtonDown("Jump"))
   {
      var bullit = Instantiate(YourPrefab, Vector3(x,y,z) , Quaternion.identity); 
      bullit.rigidbody.AddForce(transform.forward * 200);

   }
}

4. Drag your javascript to your Main Character
5. Drag your fireball prefab into your Main Character variable "YourPrefab"
6. Select your prefab, Component -> physics -> rigid body
7. Click play, you should be able to shoot fireball with your main character

the script check if the button "spacebar" is press, then it will create a new prefab(your fireball) out of thin air at the location you specify and will move with the force of 200 

No comments:

Post a Comment