Changing Player's position with one or two left mouse clicks.

Hi. I’m experimenting with making it so that the first left mouse click has the player appear at one Vector3,
and then the second left mouse click makes the player appear at a different Vector3.

It sounds simple, and I am definitely not a good coder, but have been trying to teach myself for awhile.

I’ve gone through two approaches to the code so far, which I’ll add so people can see my approaches.

Any feedback about potential directions to apply would be welcome.

using UnityEngine;
using System.Collections;

public class TwoClicks : MonoBehaviour {
    public GameObject player;



    public bool buttonPressed;

    // Use this for initialization
    void Start () {
   
        buttonPressed = false;

}

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

        if (Input.GetMouseButtonDown(0))
        {
            player.transform.position = new Vector3(3, 3, 0);
            buttonPressed = true;
            SecondClick();
           

        }
    }
        public void SecondClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            player.transform.position = new Vector3(-3, 2, 0);
        }
    }
    }
using UnityEngine;
using System.Collections;

public class UpButton : MonoBehaviour
{

    public GameObject player;
    public Vector3 Initial = player.transform.position; 

    public enum State
    {
        Idle,
        StageOne,
        StageTwo
    }
    public State _state;

    // Use this for initialization
    void Start()
    {

        GetComponent<Transform>();

        switch (_state)
        {
            case State.Idle:
                Idle();
                break;
            case State.StageOne:
                StageOne();
                break;
            case State.StageTwo:
                StageTwo();
                break;
        }


    }

    public void Idle()
    {
        player.transform.position = Initial;
        if (Input.GetMouseButtonDown(0))
        {
            StageOne();
        }
    }
    public void StageOne()
    {

        player.transform.position = new Vector3(3, 3, 0);
        if (Input.GetMouseButtonDown(0))
        {
            StageTwo();
        }
    }
   
    public void StageTwo()
    {
      
            player.transform.position = new Vector3(-3, 2, 0);
        }
    }

There are some merit to both ways you attempted, but both have flaws that will prevent it from working. You’ve got some basic state machine logic going on, but it doesn’t quite come together as you’re missing an Update function to drive the state behaviors each frame.

Try something like this, which combines a little of both:

private int pressCount;

void Update() {
    if(Input.GetMouseButtonDown(0)) {
        pressCount++;
    }

    switch(pressCount) {
        case 1:
            player.transform.position = new Vector3(3, 3, 0);
            break;
        case 2:
            player.transform.position = new Vector3(-3, 2, 0);
            pressCount = 0; // reset
            break;
    }
}
1 Like

I think one important aspect to consider is that the time is important factor to know if an user input double or single click. So you should use an attribute that will indicate the maximum time interval between clicks to be considerated double:

public class MouseClickManager : MonoBehaviour {
    private float maxTime; // maximum time between clicks to be considerated double.
    private float lastClickTime;

    void Start () {
        maxTime = 1.0f;   // one second
        lastClickTime = 0;
    }
  
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            if (Time.time - lastClickTime < maxTime)
                Debug.Log("Double click");
            else
                Debug.Log("Single click");
      
            lastClickTime = Time.time;
        }
    }
}

The first attribute maxTime will store the maximum time in seconds between both clicks to consider that action a double click. The second attribute lastClickTime will just store the exact time at which was clicked the last time. In the Start function we just initialize the value of maxTime to 1.0f so that means the maximum time between clicks is one second.
The initalization of the next attribute is just for avoiding an error when we click the first time.

The important things happen at the Update function. If the user input a click, its checked if the actual time minus the last clicked time is less than the maximum time. If yes then that is a double click. And finally we always save the time of the last click.

I hope I have clarified you and help you to be better programmer and happy Unitying!! Regards, Rudy :wink:

2 Likes

Thanks so much for taking the time to help me and giving me some insight and direction! Many thanks!

1 Like