Search Unity

Coin Magnet

Discussion in 'Getting Started' started by joytdecastro, Mar 4, 2015.

  1. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    i had a coin magnet in my game, but when my character pass through the 2nd magnet, it won't collect coins, can someone help me? here's my code for the character

    Code (JavaScript):
    1. #pragma strict
    2. static var attractCoins: boolean = false;
    3. var timer = 15.0;
    4. var icon : Texture2D;
    5. var collect = 5.0;
    6. var isGet : boolean = false;
    7.  
    8. //Invoke("Collect", collect);
    9.  
    10. function Start () {
    11.     attractCoins = false;
    12. }
    13.  
    14. function OnTriggerEnter(info : Collider)
    15. {
    16.     //isGet = true;
    17.         if(info.tag == "Magnet")
    18.         {
    19.             attractCoins = true;
    20.        
    21.         }
    22. }
    23. function Update () {
    24.  
    25.    
    26.     if (attractCoins)
    27.     {
    28.         timer -= Time.deltaTime;
    29.             if (timer <= 0)
    30.             {
    31.                 timer = 0;
    32.                 attractCoins = false;
    33.                 Collect();
    34.                
    35.             }
    36.    
    37.     }
    38.    
    39. }
    40.  
    41.  
    42. function OnGUI(){
    43.    
    44.  
    45.     if (attractCoins)
    46.     {
    47.         GUI.Box (Rect (10,Screen.height - 120,85,105), "Magnet");
    48.         (GUI.Button (Rect (20,Screen.height - 95, 60, 60), icon));
    49.         GUI.Label(Rect(25,Screen.height - 35,110,50), "Timer:" + timer.ToString("0"));
    50.     }
    51.    
    52. }
    53. function Collect(){
    54. Debug.Log("collect");
    55.     isGet = true;
    56. }
    57.  
    and here's for coin script

    Code (JavaScript):
    1. #pragma strict
    2. var target : GameObject;
    3.  
    4. function Start () {
    5.     target = GameObject.FindGameObjectWithTag("Player");
    6.  
    7. }
    8. function Update () {
    9.     if(car.attractCoins == true && Vector3.Distance(transform.position,target.transform.position) < 5.0)
    10.     {
    11.         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * 5);
    12.     }
    13. }
    hope that you'll help me .thanks
     
    Last edited: Mar 4, 2015
  2. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi joy,

    I am not that familiar with Javascript, but in looking at the code I was wondering... when should the timer be decreasing itself and how long should the timer run before collecting the coins? Should the timer be reset to 15.0f instead of 0.0f in the Update function?

    Like, changing this code from this:

    Code (JavaScript):
    1. function Update () {
    2.  
    3.     if (attractCoins)
    4.     {
    5.         timer -= Time.deltaTime;
    6.             if (timer <= 0)
    7.             {
    8.                 timer = 0;
    9.                 attractCoins = false;
    10.                 Collect();
    11.              
    12.             }
    13.  
    14.     }
    15.  
    16. }
    To this:

    Code (JavaScript):
    1. function Update () {
    2.  
    3.  
    4.     if (attractCoins)
    5.     {
    6.         timer -= Time.deltaTime;
    7.             if (timer <= 0)
    8.             {
    9.                 timer = 15; // Set the timer to the original value.
    10.                 attractCoins = false;
    11.                 Collect();
    12.              
    13.             }
    14.  
    15.     }
    16.  
    17. }
     
    Last edited: Mar 5, 2015
  3. Aafreen

    Aafreen

    Joined:
    May 27, 2017
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class SilverCoin : MonoBehaviour {

    GameObject[] currentCoins;

    public bool coinMag = false;
    public float Timer = 0.0f;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {


    if (coinMag) {
    Timer += 1 * Time.deltaTime;
    if (Timer >= 10) {
    coinMag = true;
    Timer = 0;
    }

    }


    GameObject P = GameObject.FindGameObjectWithTag ("Player");
    currentCoins = GameObject.FindGameObjectsWithTag("SilverCoin");

    GameObject M = GameObject.FindGameObjectWithTag ("SilverMagnet");
    foreach (GameObject Coin in currentCoins) {

    if (Vector3.Distance (Coin.transform.position, P.transform.position) < 0.5) {

    Destroy (Coin.gameObject);

    }

    if (coinMag) {

    if (Vector3.Distance (Coin.transform.position, P.transform.position) < 5) {


    Coin.transform.Translate ((P.transform.position - Coin.transform.position).normalized * 1 *Time.deltaTime,Space.World);

    }

    }
    }
    if (M != null) {


    if (Vector3.Distance (M.transform.position, P.transform.position) < 0.5) {
    Debug.Log (".......................................");
    Destroy (M.gameObject);

    if (coinMag == false) {

    coinMag = true;
    }

    }

    }


    }
    }