Skip to content

Commit

Permalink
Added Direct Drag movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickk888SAMP committed Jul 13, 2024
1 parent f6bab25 commit d826efa
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
"Temp/": true
},
"git.ignoreLimitWarning": true,
"dotnet.defaultSolution": "RTSCameraController.sln"
"dotnet.defaultSolution": "RTSCameraController-Cinemachine.sln"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1260,13 +1260,14 @@ MonoBehaviour:
AllowTiltRotate: 1
AllowZoom: 1
AllowDragMove: 1
mouseDragStyle: 0
AllowKeysMove: 1
AllowScreenSideMove: 1
AllowHeightOffsetChange: 1
MouseLockOnRotate: 1
InvertMouseVertical: 0
InvertMouseHorizontal: 0
CameraMouseSpeed: 2
CameraMouseSpeed: 8
CameraRotateSpeed: 2
CameraKeysRotateSpeedMultiplier: 50
CameraKeysSpeed: 6
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions Assets/Nickk888/RTSCameraController/Scenes/ExampleScene.unity
Git LFS file not shown
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ public class OnRotateHandledEventArgs : EventArgs
public event EventHandler<OnMouseDragStartedEventArgs> OnMouseDragStarted;
public class OnMouseDragStartedEventArgs : EventArgs
{
public MouseDragStyle mouseDragStyle;
public Vector2 mouseLockPosition;
}
public event EventHandler OnMouseDragStopped;
public event EventHandler<OnMouseDragHandledEventArgs> OnMouseDragHandled;
public class OnMouseDragHandledEventArgs : EventArgs
{
public MouseDragStyle mouseDragStyle;
public bool isMoving;
public Vector2 mousePosition;
public Vector2 vectorChange;
}

public event EventHandler<OnZoomHandledEventArgs> OnZoomHandled;
Expand Down Expand Up @@ -81,6 +84,10 @@ [SerializeField] [Tooltip("Allows or Disallows Zooming.")]
[SerializeField] [Tooltip("Allows or Disallows mouse drag movement.")]
public bool AllowDragMove = true;

public enum MouseDragStyle { MouseDirection, Direct }
[SerializeField] [Tooltip("The style of mouse Drag.")]
private MouseDragStyle mouseDragStyle = MouseDragStyle.MouseDirection;

[SerializeField] [Tooltip("Allows or Disallows camera movement with keys/gamepad input.")]
public bool AllowKeysMove = true;

Expand All @@ -99,9 +106,11 @@ [SerializeField] [Tooltip("Invert the vertical mouse input?")]
[SerializeField] [Tooltip("Invert the horizontal mouse input?")]
public bool InvertMouseHorizontal = false;



[Space] [Header("Speed")]
[SerializeField, Min(0)]
public float CameraMouseSpeed = 2.0f;
public float CameraMouseSpeed = 8.0f;

[SerializeField, Min(0)]
public float CameraRotateSpeed = 2.0f;
Expand Down Expand Up @@ -219,8 +228,8 @@ private void Awake()
_cinemachineBrain = _cam.gameObject.GetComponent<CinemachineBrain>();
else
Debug.LogError("Main Camera wasn't found. Can't get the Cinemachine Brain.");
if(Instance is not null)
Destroy(Instance);
// if(Instance is not null)
// Destroy(Instance);
Instance = this;
}

Expand Down Expand Up @@ -359,32 +368,62 @@ private void HandleMouseDrag(Vector3 mousePos)
{
if (!_isRotating && !_isSideZoneMoving)
{
if (_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging)
{
_mouseLockPos = mousePos;
_isDragging = true;
CancelTargetLock();
OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos });
}
if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove))
switch(mouseDragStyle)
{
Cursor.visible = true;
_isDragging = false;
OnMouseDragStopped?.Invoke(this, EventArgs.Empty);
}
if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove)
{
Vector3 vectorChange = new Vector3(_mouseLockPos.x - mousePos.x, 0, _mouseLockPos.y - mousePos.y) * -1;
float distance = vectorChange.sqrMagnitude;
bool canMove = distance > (CameraDragDeadZone * CameraDragDeadZone);
Cursor.visible = !canMove;

// Move target relative to Camera
if (canMove)
MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed / 100);

OnMouseDragHandled?.Invoke(this, new OnMouseDragHandledEventArgs { isMoving = canMove, mousePosition = mousePos });
case MouseDragStyle.MouseDirection:
if (_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging)
{
_mouseLockPos = mousePos;
_isDragging = true;
CancelTargetLock();
OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos, mouseDragStyle = MouseDragStyle.MouseDirection });
}
if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove))
{
Cursor.visible = true;
_isDragging = false;
OnMouseDragStopped?.Invoke(this, EventArgs.Empty);
}
if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove)
{
Vector3 vectorChange = new Vector3(_mouseLockPos.x - mousePos.x, 0, _mouseLockPos.y - mousePos.y) * -1;
float distance = vectorChange.sqrMagnitude;
bool canMove = distance > (CameraDragDeadZone * CameraDragDeadZone);
Cursor.visible = !canMove;

// Move target relative to Camera
if (canMove)
MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed / 100);

OnMouseDragHandled?.Invoke(this, new OnMouseDragHandledEventArgs { isMoving = canMove, mousePosition = mousePos });
}
break;
case MouseDragStyle.Direct:
if(_inputProvider.DragButtonInput() && AllowDragMove && !_isDragging)
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
_isDragging = true;
CancelTargetLock();
OnMouseDragStarted?.Invoke(this, new OnMouseDragStartedEventArgs { mouseLockPosition = mousePos, mouseDragStyle = MouseDragStyle.Direct });
}
if ((_isDragging && !_inputProvider.DragButtonInput()) || (_isDragging && !AllowDragMove))
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
_isDragging = false;
OnMouseDragStopped?.Invoke(this, EventArgs.Empty);
}
if (_inputProvider.DragButtonInput() && _isDragging && AllowDragMove)
{
Vector3 vectorChange = _inputProvider.MouseInput();
vectorChange.z = vectorChange.y;
vectorChange.y = 0;
MoveTargetRelativeToCamera(vectorChange, CameraMouseSpeed);
}
break;
}

}
}

Expand Down
18 changes: 12 additions & 6 deletions Assets/Nickk888/RTSCameraController/Scripts/RTSCanvasController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,25 @@ private void RTSCameraTargetController_OnRotateStarted(object sender, EventArgs

private void RTSCameraTargetController_OnMouseDragStarted(object sender, RTSCameraTargetController.OnMouseDragStartedEventArgs e)
{
mouseDragStartPoint.transform.position = new Vector2(e.mouseLockPosition.x, e.mouseLockPosition.y);
mouseDragCanvasGameObject.SetActive(true);
if(e.mouseDragStyle == RTSCameraTargetController.MouseDragStyle.MouseDirection)
{
mouseDragStartPoint.transform.position = new Vector2(e.mouseLockPosition.x, e.mouseLockPosition.y);
mouseDragCanvasGameObject.SetActive(true);
}
}

private void RTSCameraTargetController_OnMouseDragStopped(object sender, EventArgs e)
=> mouseDragCanvasGameObject.SetActive(false);

private void RTSCameraTargetController_OnMouseDragHandled(object sender, RTSCameraTargetController.OnMouseDragHandledEventArgs e)
{
mouseDragEndPoint.transform.position = e.mousePosition;
Vector3 dir = (Vector3)e.mousePosition - mouseDragStartPoint.transform.position;
mouseDragEndPoint.transform.right = dir;
mouseDragEndPoint.SetActive(e.isMoving);
if (e.mouseDragStyle == RTSCameraTargetController.MouseDragStyle.MouseDirection)
{
mouseDragEndPoint.transform.position = e.mousePosition;
Vector3 dir = (Vector3)e.mousePosition - mouseDragStartPoint.transform.position;
mouseDragEndPoint.transform.right = dir;
mouseDragEndPoint.SetActive(e.isMoving);
}
}

private void RTSCameraTargetController_OnZoomHandled(object sender, RTSCameraTargetController.OnZoomHandledEventArgs e)
Expand Down

0 comments on commit d826efa

Please sign in to comment.