Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Minecraft like mining.

Discussion in 'Scripting' started by Ncon123, Aug 21, 2015.

  1. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    I understand the problem is there a way around it though?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public float MineSpeed = 1;
    7.  
    8.     public GameObject ItemHit;
    9.  
    10.     public Item ItemScript;
    11.  
    12.  
    13.     void Start ()
    14.     {  
    15.  
    16.     }
    17.    
    18.     void Update ()
    19.     {
    20.         StartCoroutine(playerHit());
    21.     }
    22.  
    23.     IEnumerator playerHit()
    24.     {  
    25.         RaycastHit hit;  
    26.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.  
    28.         if (Physics.Raycast(ray, out hit, 50))
    29.         {  
    30.  
    31.             ItemHit = hit.transform.gameObject;
    32.             ItemScript  = ItemHit.GetComponent<Item>();
    33.  
    34.             if (Input.GetMouseButton(0))
    35.             {
    36.                 yield return new WaitForSeconds(ItemScript.ItemMiningSpeed * MineSpeed);
    37.  
    38.                 if (Input.GetMouseButton(0))
    39.                 {
    40.                     Destroy(ItemScript.ThisItem);
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
    46.  
    After waiting and the object is destroyed every object I look at after this while holding mouse down is destroyed instantly.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Try to start the function in the start, not in update
     
  3. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    *Bump*
     
  4. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    *Bump*
     
  5. Ncon123

    Ncon123

    Joined:
    May 6, 2015
    Posts:
    25
    New code on the *bump* threaad
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You don't need to bump the thread the day you uploaded it, and did you read my answer?
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You shouldn't be calling StartCoroutine like that in Update.
    Execution in start won't help either as it will never be called again once it is done.

    You should add some more logic to start and stop your coroutine. Also, with the current code you wouldn't need to hold down the mouse button while the yielding takes place. You'd rather want to check the buttonDown thing frequently and if it's not down, cancel the "mining process".
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Mining in mimecraft works like:
    1.gets the mining time of the block, that you look at
    2.checks if mouse is down, and looking at the same block like before
    3.if yes, adds the spent time to the counter, if no, resets the timer
    4.if timer reached mimingtime, resets timer, mines block.
     
    Gerald Tyler likes this.