- Home /
How do i check if mouse moved a certain distance?
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
Answer by Mmmpies · Feb 07, 2015 at 06:42 PM
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");
}
}
Thanks man but i figured it out in a different way :)
No problem @Ramish do you want to post your solution, if you found a different/better way of doing it it may help others.
Yeah I posted my method below.$$anonymous$$y method is a bit weird but it works :D @$$anonymous$$mmpies
Answer by $$anonymous$$ · Feb 07, 2015 at 07:31 PM
Got it to work :D 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);
}
}
}
Your answer
Follow this Question
Related Questions
focusing on a unit during unit detection in an RTS 1 Answer
Move to and stop 1 Answer
Stopping navmesh agent next to the enemy 0 Answers