Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // Tree Chopping - Things you'll need.
  2.  
  3. // This will check if you hit something by casting a circle in front of you. Variables aren't correct but its something like that
  4. // it'll tell you. Put this in your Player.cs script.
  5. RaycastHit2D ray = Physics2D.CircleCast (transform.position, radius, range);
  6.  
  7. // Check if you hit a tree and then tell the tree you want to gain a resource by using its GainResource () function.
  8. void OnTriggerEnter2D (Collider col) {
  9. if (col.tag == "Tree") {
  10. col.transform.SendMessage ("GainResource");
  11. }
  12. }
  13.  
  14. // Your tree will need something like this.
  15. public int totalResource = 5;
  16. int currentResouce;
  17.  
  18. void Start () {
  19. currentResource = totalResource;
  20. }
  21.  
  22. void GainResource () {
  23. if (currentResource > totalResource) {
  24. // Get your player here and send them the resource.
  25. GameObject Player = GameObject.FindGameObjectWithTag ("Player");
  26. player.GetComponent<ResourceController>().IncreaseWood(1);
  27. } else {
  28. DestroyTree();
  29. }
  30. }
  31.  
  32. void DestroyTree () {
  33. Destroy(gameObject);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement