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. Dismiss Notice

Change value on mouse drag

Discussion in 'Scripting' started by Elverion, Feb 1, 2016.

  1. Elverion

    Elverion

    Joined:
    Feb 1, 2016
    Posts:
    5
    Hi everybody !

    First i'm french guy and my english is not perfect, sorry for mistakes :p

    I'm a begginer on Unity and i try to understand how programming. Currently, i try to make a Clicker Game for fun
    but i have a problem : For my Clicker Game i don't want add value on mouse click but when you drag the mouse on a sprite. Now my script is like that :

    using UnityEngine;
    using System.Collections;

    public class Click : MonoBehaviour
    {
    public UnityEngine.UI.Text GpC;
    public UnityEngine.UI.Text GoldDisplay;
    public float Gold = 0.00f;
    public int GoldPerClick = 1;
    public var drag: float;

    void Update ()
    {
    GoldDisplay.text = "Gold : " + Gold.ToString("F0");
    GpC.text = GoldPerClick + " Gold / Click";
    }

    public void MouseDrag ()
    {
    Gold += GoldPerClick ;
    }
    }

    I have others scripts for buy Gold per Sec and Gold buy Click but i think it's not necessary for my problem

    After i add on a sprite a component event trigger/drag/ and i add my script.
    It's work (yes ! :D) but problem is my gold per sec is 1 but when i drag on my sprite, Unity add with the speed of the mouse so you add 50-100 gold each sec.

    I want (in begin of game) add 1 gold each sec whatever your move speed with mouse

    I try to understand how WaitForSecond and StartCoroutines works but i don't understand, or maybe another option more simpliest probably exist

    Thanks you in advance everybody :)
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    First get time mouse started dragging.
    Check how long passed since user started dragging. Also check mouse distance passed (user might've just clicked and didn't move mouse - that's drag too). Add gold depending on that.
     
  3. Elverion

    Elverion

    Joined:
    Feb 1, 2016
    Posts:
    5
    Sorry but i'm very begginer, where is time mouse for you ?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    What he means is:
    When the mouse starts dragging, record the time (you get it from Time.time) that happened. You can compare against that stored value later, and see how many seconds it has been since he started clicking.