- Home /
How to translate something only on X axis on Y axis,once per mouse click?
So do you know the game Find a way, Juan?
It's a puzzle game,and I want to make somehow a copy of that,for a school project.
The thing is, once i click the mouse,i want the puzzle things to move ONLY left/right or up/down depending on if i started to move the mouse sideways or up and down. For example i want to move a piece a bit on the right. I click it,and move the piece to right. Only the X axis changes,the Y stays constant. If i want to move something,up or down,the X stays constant.
using UnityEngine; using System.Collections; public class slide : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(Input.GetMouseButton(0)){ Vector3 initialPosition = this.transform.localPosition; Vector3 myPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition)-this.transform.localPosition; Vector3 sndPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - this.transform.localPosition; if (Mathf.Abs(initialPosition.x-myPosition.x - sndPosition.x) > Mathf.Abs(initialPosition.y-myPosition.y - sndPosition.y)) { myPosition.y = 0; } else { myPosition.x = 0; } myPosition.z = 0; transform.Translate(myPosition); } } }
The thing is,I cant fighre it out,how to stop checking the absolute values once the mouse has moved.
Answer by SkaredCreations · Mar 22, 2014 at 07:42 PM
For this kind of behavior I'd suggest to store initialPosition when GetMouseButtonDown (declaring it as private variable in the script, not local in the function) and apply your other code when GetMouseButtonUp.
I cant apply my code when Get$$anonymous$$ouseButtonUp. The pieces of the puzzles should be dragged. The mouse button is pressed during the dragging