- Home /
Why does this not detect mouse movement?
I have tried many ways to detect mouse movement and have been S.O.L. getting anywhere with it. I can detected the current location and compare it to the original location, but this doesn't seem to work for detecting mouse movement. I tried get axis and that isn't working either. Am I doing something wrong?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UserInput : MonoBehaviour {
Vector2 beginMousePos;
Vector2 currMousePos;
bool mouseIsSet = false;
bool mouseHasMoved = false;
RaycastHit hit;
bool UnitsAreSelected = false;
bool structSelected= false;
// Unit Menu Option Icons
Vector2 attackIconPos;
Vector2 patrolIconPos;
Vector2 followIconPos;
Vector2 loadIconPos;
Vector2 unloadIconPos;
public Texture2D attactIcon;
public Texture2D patrolIcon;
public Texture2D followIcon;
public Texture2D loadIcon;
public Texture2D unloadIcon;
// Use this for initialization
void Start () {
attackIconPos = new Vector2((Screen.width/15), (Screen.height/9)*7);
patrolIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*7);
followIconPos = new Vector2((Screen.width/15)*3, (Screen.height/9)*7);
loadIconPos = new Vector2((Screen.width/15), (Screen.height/9)*8);
unloadIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*8);
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown (0))
{
if (!mouseIsSet)
{
Debug.Log (mouseIsSet);
beginMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
mouseIsSet = true;
}
if(Input.GetAxis("Mouse X") < 0)
{
Debug.Log ("movedmouse");
mouseHasMoved = true;
}
else if (Input.GetAxis("Mouse X") > 0)
{
Debug.Log ("movedmouse");
mouseHasMoved = true;
}
else if(Input.GetAxis("Mouse Y") < 0)
{
Debug.Log ("movedmouse");
mouseHasMoved = true;
}
else if (Input.GetAxis("Mouse Y") > 0)
{
Debug.Log ("movedmouse");
mouseHasMoved = true;
}
}
if(Input.GetMouseButtonUp (0))
{
if (mouseHasMoved)
{
mouseIsSet = false;
mouseHasMoved = false;
currMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
UnitManager.FindSelectedUnits(beginMousePos, currMousePos);
if (UnitManager.Selected.Count > 0)
{
UnitsAreSelected = true;
}
else
{
UnitsAreSelected = false;
}
}
else
{
mouseIsSet = false;
}
}
if(Input.GetMouseButtonDown (1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
Unit unitHit = hit.collider.gameObject.GetComponentInChildren<Unit>();
if(unitHit != null)
{
UnitManager.MoveActSelectedUnits(unitHit);
}
else
{
UnitManager.MoveSelectedUnits (hit.point);
}
}
}
}
void OnGUI()
{
if (UnitsAreSelected)
{
if (!structSelected)
{
// Attack
if (GUI.Button(new Rect(attackIconPos.x, attackIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Patrol
if (GUI.Button(new Rect(patrolIconPos.x, patrolIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Follow
if (GUI.Button(new Rect(followIconPos.x, followIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Load
if (GUI.Button(new Rect(loadIconPos.x, loadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// UnLoad
if (GUI.Button(new Rect(unloadIconPos.x, unloadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
}
else
{
}
}
}
}
Ins$$anonymous$$d of checking both X and Y to see if your mouse has moved, use Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Store the first click in a Vector2 then make an ifstatement with Vector2.Distance(originalPos, newPos) > 0 - $$anonymous$$ouseHas$$anonymous$$oved = true then set new location to newPos, think that will solve your problem! Then create a rectangle to select units between your originalPos and your newPos!
I tried this originally, Just in case I did something wrong I will try again. Thanks for the quick reply
Answer by opdday2 · Mar 05, 2014 at 08:21 PM
ok, so Now I have this and it doesn't seem to even run logic in GetMouseButtonDown(0) here is what I have now and the debug.log at beginning of GetMouseButtonDown(0) is never called. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class UserInput : MonoBehaviour {
Vector2 beginMousePos;
Vector2 currMousePos;
bool mouseIsSet = false;
RaycastHit hit;
bool UnitsAreSelected = false;
bool structSelected= false;
// Unit Menu Option Icons
Vector2 attackIconPos;
Vector2 patrolIconPos;
Vector2 followIconPos;
Vector2 loadIconPos;
Vector2 unloadIconPos;
public Texture2D attactIcon;
public Texture2D patrolIcon;
public Texture2D followIcon;
public Texture2D loadIcon;
public Texture2D unloadIcon;
// Use this for initialization
void Start () {
attackIconPos = new Vector2((Screen.width/15), (Screen.height/9)*7);
patrolIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*7);
followIconPos = new Vector2((Screen.width/15)*3, (Screen.height/9)*7);
loadIconPos = new Vector2((Screen.width/15), (Screen.height/9)*8);
unloadIconPos = new Vector2((Screen.width/15)*2, (Screen.height/9)*8);
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown (0))
{
Debug.Log ("buttonDowm");
if (!mouseIsSet)
{
beginMousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Debug.Log (beginMousePos);
mouseIsSet = true;
}
}
if(Input.GetMouseButtonUp (0))
{
if (mouseHasMoved())
{
currMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
UnitManager.FindSelectedUnits(beginMousePos, currMousePos);
if (UnitManager.Selected.Count > 0)
{
UnitsAreSelected = true;
}
else
{
UnitsAreSelected = false;
}
mouseIsSet = false;
}
else
{
mouseIsSet = false;
}
}
if(Input.GetMouseButtonDown (1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
Unit unitHit = hit.collider.gameObject.GetComponentInChildren<Unit>();
if(unitHit != null)
{
UnitManager.MoveActSelectedUnits(unitHit);
}
else
{
UnitManager.MoveSelectedUnits (hit.point);
}
}
}
}
void OnGUI()
{
if (UnitsAreSelected)
{
if (!structSelected)
{
// Attack
if (GUI.Button(new Rect(attackIconPos.x, attackIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Patrol
if (GUI.Button(new Rect(patrolIconPos.x, patrolIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Follow
if (GUI.Button(new Rect(followIconPos.x, followIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// Load
if (GUI.Button(new Rect(loadIconPos.x, loadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
// UnLoad
if (GUI.Button(new Rect(unloadIconPos.x, unloadIconPos.y, Screen.width/15, Screen.height/9), attactIcon))
{
}
}
else
{
}
}
}
bool mouseHasMoved()
{
if(Vector2.Distance(beginMousePos, new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y)) > 0)
{
Debug.Log ("movedmouse");
return true;
}
else
{
return false;
}
}
}
the logic in Get$$anonymous$$ouseButtonDown (1) RUNS FINE
Your answer
Follow this Question
Related Questions
Why is OnDrag triggering but not OnMouseDown? 0 Answers
Can I change GUIButton behaviour? 1 Answer
Prevent Input.GetMouseButtonDown from working anywhere. 3 Answers
How can I move my 2D main player up and down by clicking and dragging him up or down? 1 Answer
Click and drag to rotate an object without changing its position. 2 Answers