-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotatingObject.cs
91 lines (73 loc) · 2.56 KB
/
RotatingObject.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotatingObject : MonoBehaviour
{
Ray ray;
RaycastHit hit;
public GameObject prefab;
Vector3 screenPoints;
GameObject selectedObject;
Vector3 offset;
public int rotationSpeed = 10;
int temp = 0;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
/*
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0) && temp == 0)
{
// Instantiate new prefab with every left click
GameObject obj = Instantiate(prefab, new Vector3(hit.point.x, hit.point.y, hit.point.z), Quaternion.identity) as GameObject;
screenPoints = Camera.main.WorldToScreenPoint(transform.position);
temp++;
}
}
*/
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
{
selectedObject = hit.collider.gameObject;
Debug.Log("In Mouse Down");
}
}
if (selectedObject != null)
{
screenPoints = Camera.main.WorldToScreenPoint(selectedObject.transform.position);
offset = selectedObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y, screenPoints.z));
Debug.Log("Object Selected");
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//float mouseX = Input.GetAxis("Mouse X") * rotationSpeed * Mathf.Deg2Rad;
transform.Rotate(Vector3.up* rotationSpeed);
}
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
//float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed * Mathf.Deg2Rad;
transform.Rotate(Vector3.down * rotationSpeed);
}
}
/*
public void OnMouseDown()
{
}
*/
public void OnMouseDrag()
{
if (selectedObject != null)
{
Vector3 CursorScreenPoints = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoints.z);
Vector3 cursorPoints = Camera.main.ScreenToWorldPoint(CursorScreenPoints) + offset;
selectedObject.transform.position = cursorPoints;
}
}
private void OnMouseOver()
{
}
}