- Home /
Question by
Arlatan · Jul 14, 2020 at 10:49 AM ·
viewportmouse positionmouse-drag
Determine 2d direction between mouse click and release.
Hello.
I am trying to determine the direction between two mouse clicks with the initial click being the origin and the second click being the final position. I was trying to do this in Viewport. The goal is to determine weapon swing angles.
I'm I am able to find it in four quadrants, but I want to add four more, and am just getting confused. Here is what I have.
using UnityEngine;
public class AttackController : MonoBehaviour
{
Camera myCamera;
Vector3 firstPoint;
Vector3 secondPoint;
Vector3 point;
void Start()
{
myCamera = this.GetComponent<Camera>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 firstPoint = myCamera.ScreenToViewportPoint(Input.mousePosition);
Debug.Log("Mouse button is down... first point is " + firstPoint);
}
if (Input.GetMouseButtonUp(0))
{
Vector3 secondPoint = myCamera.ScreenToViewportPoint(Input.mousePosition);
Debug.Log("Mouse button is up... second point is " + secondPoint);
Vector3 point = secondPoint - firstPoint;
Debug.Log("point = " + point);
if (point.x >= .5 && point.y <= .5)
{
print("Lower Right Animation");
}
else if (point.x >= .5 && point.y >= .5)
{
print("Upper Right Animation");
}
else if (point.x <= .5 && point.y >= .5)
{
print("Upper Left Animation");
}
else if (point.x <= .5 && point.y <= .5)
{
print("Lower Left Animation");
}
So the goal would be to add a left to right, right to left, up to down, and down to up swing.
Any suggestions would be greatly appreciated! It took me awhile to get this to work, and I'm just burned out at the moment and maybe just need to take a break.
Comment