How do i check if mouse moved a certain distance in a direction while holding left/right click button.for example i click on an object and move my mouse in right direction the object rotates 180 degrees to right when i leave the mouse click but if i don’t reach that certain distance the object doesn’t rotate? please help i can’t seem to figure this out on my own
Try this:
using UnityEngine;
using System.Collections;
public class MouseDownDist : MonoBehaviour {
private Vector3 mouseDragStart;
private Vector3 mouseDragEnd;
private float mouseDistance;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
{
mouseDragStart = Input.mousePosition;
}
if(Input.GetMouseButtonUp(0))
{
mouseDragEnd = Input.mousePosition;
mouseDistance = mouseDragEnd.x - mouseDragStart.x;
if(mouseDistance > 200)
Rotate();
}
}
void Rotate()
{
Debug.Log("Rotating object");
}
}
Got it to work This should work only for right rotation(just need to change vales for other directions)
some brackets might be missing
public class Rotation: MonoBehaviour {
Vector3 MaximumX;
Vector3 mousepostion;
Vector3 Cmouseposition;
void start(){
if (Input.GetMouseButtonDown (0)) {
CheckTouch (Input.mousePosition, "began");
}
if (Input.GetMouseButtonUp (0)) {
if (Cmouseposition.x > MaximumX.x) {
// print (Cmouseposition.x);
Debug.LogError("Hey");
}
CheckTouch (Input.mousePosition, "ended");
}
}
void CheckTouch(Vector3 pos, string phase)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit.gameObject.name == "TestX" && hit && phase == "began")
{
;
Vector3 mousepostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
MaximumX = mousepostion;
MaximumX+=new Vector3(10,0,0);
//print(mousepostion.x);
//print(MaximumX.x);
}
}
}