Wednesday, 27 March 2013

Picking Up Stuff Script!

using UnityEngine;
using System.Collections;
public class PickupObject : MonoBehaviour
{

 public int distanceToTestFor = 10;
 private int foodLayerMask = 1 << 8;  // food layer is on 8, so create a bitmask
 private Vector3 OFF_SCREEN_POSITION = new Vector3(0, -100, 0 );

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update ()
 {
  RaycastHit hit;

  if ( Input.GetMouseButtonDown( 0 ) )
  {
   // cast a ray from the current screen position into the game world
   // to test if it hits an object that we can pick up
 
   if ( Physics.Raycast(Camera.main.ScreenPointToRay( Input.mousePosition ),out hit, distanceToTestFor, foodLayerMask ))
   {
//    hit.transform.position = OFF_SCREEN_POSITION; - this is only needed if you want to respawn the food
//    hit.transform.gameObject.SendMessage("Respawn");
  
    Destroy( gameObject );
   }
 
  }

 }

}

This is the script to be able to actually click on the food (plants) and be able to pick them up. We could use the pool of objects manager script from our coding lessons to respawn the food, but with this code, we will only be able to have lots of food sources which will then be "destroyed" when clicked on.  

I have put up the excersise we did in the coding session today on Dropbox, called excersise10.2, hopefully you will be able to see what we did with all the codes and that.

No comments:

Post a Comment