-
Notifications
You must be signed in to change notification settings - Fork 2
/
RotateScript.cs
76 lines (56 loc) · 2.26 KB
/
RotateScript.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//Attached to the ramp in order to rotate it around the x axis.
//rampAngle stores the current angle in degrees for easy output to the UI
public class RotateScript : MonoBehaviour {
public Transform ramp; //the attached ramp that will be rotated by this handle
public Transform rampContainer; //the empty game object containing the ramp
bool handleClicked; //whether the mouse is clicking the rotate handle or not
public float angleOffset = 0;
private Quaternion rampRotation;
public float deltaAngle;
public Text angleText; //the textbox showing the ramp angle
// Use this for initialization
void Start () {
rampRotation = ramp.rotation;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0) && handleClicked == true)
{
if(Input.GetAxis("Mouse Y") > 0) //mouse moving up
{
deltaAngle += 0.1f;
}
else if(Input.GetAxis("Mouse Y") < 0 && ramp.position.y > 0) //mouse moving down, limited to 0 degrees
{
deltaAngle -= 0.1f;
}
Quaternion deltaQuat = Quaternion.AngleAxis(deltaAngle, Vector3.right);
rampContainer.rotation = rampRotation * deltaQuat; //multiplying quaternions adds the rotations
}
OutputRampAngle(); //output the ramp angle in degrees to the screen
}
private void OnMouseDown()
{
handleClicked = true;
}
private void OnMouseUp()
{
handleClicked = false;
}
//The ramp angle in world space is the sum of the ramp angle and the container that it is inside.
public float GetRampAngle()
{
//float rampContainerAngle = Quaternion.Angle(rampContainer.rotation, Quaternion.identity);
float rampAngle = Quaternion.Angle(ramp.rotation, Quaternion.identity);
return rampAngle;
}
private void OutputRampAngle()
{
angleText.text = GetRampAngle().ToString("F1");
angleText.transform.position = Camera.main.WorldToScreenPoint(new Vector3(0,0,-1));
}
}