summaryrefslogtreecommitdiff
path: root/Other/NavMeshTest/Assets/Scripts/Prop/Switch.cs
blob: 1312616f111f5a06219b51c814ca2679d9a1d732 (plain)
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Switchable : MonoBehaviour
{
	public virtual void OnSwitch(bool on)
	{ }
}


public class Switch : MonoBehaviour
{
    public Switchable target;

    public Transform bar;

    public Vector2 rotationRange;

    private bool m_IsOn;

    private void Start()
    {
        m_IsOn = false;
        OnPressed();
    }

    private void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit info;
            if (Physics.Raycast(ray, out info))
            {
                if(info.collider.gameObject == gameObject)
                {
                    m_IsOn = !m_IsOn;

                    OnPressed();

                    if (target != null)
                    {
                        target.OnSwitch(m_IsOn);
                    }
                }
            }
        }
    }

    void OnPressed()
    {
        if(m_IsOn)
        {
            bar.localRotation = Quaternion.Euler(0, 0, rotationRange.x);
        }
        else
        {
            bar.localRotation = Quaternion.Euler(0, 0, rotationRange.y);
        }
    }

}