-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCannonRotationScript.cs
45 lines (35 loc) · 1.12 KB
/
CannonRotationScript.cs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//For rotating the cannon
public class CannonRotationScript : MonoBehaviour {
public int rotationOffset = 90;
public Text angle_text;
private bool cannonClicked;
public Transform cannonParent;
// Update is called once per frame
void Update () {
if (cannonClicked == true)
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - cannonParent.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
cannonParent.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
//update the UI to reflect the new angle
UpdateAngle(Quaternion.Angle(cannonParent.rotation, Quaternion.identity));
}
}
private void UpdateAngle(float angle)
{
angle_text.text = angle.ToString("F1");
}
private void OnMouseDown()
{
cannonClicked = true;
}
private void OnMouseUp()
{
cannonClicked = false;
}
}