-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveSwordWithMouse.cs
More file actions
32 lines (25 loc) · 1.07 KB
/
Copy pathMoveSwordWithMouse.cs
File metadata and controls
32 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using UnityEngine;
using UnityEngine.InputSystem;
public class MoveSwordWithMouse : MonoBehaviour
{
public GameObject plrRef;
public Camera cameraRef;
public float distAwayFromPlayer = 8f;
//
public PauseSystem pauseSystemScript;
//
private void Update()
{
if (pauseSystemScript.GetGamePausedState()) {return;}
Vector2 mousePos = Mouse.current.position.value;
float zDistance = Mathf.Abs(cameraRef.transform.position.z - plrRef.transform.position.z);
Vector3 thingToPass = new Vector3(mousePos.x, mousePos.y, zDistance);
Vector3 worldPos = cameraRef.ScreenToWorldPoint(thingToPass);
Vector3 direction = (worldPos - (plrRef.transform.position)).normalized;
// ---- //
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
// ---- //
this.gameObject.transform.position = (Vector2)plrRef.transform.position + (Vector2)direction * distAwayFromPlayer;
this.gameObject.transform.rotation = Quaternion.Euler(0, 0, angle);
}
}