Created
July 5, 2015 16:51
-
-
Save gotwig/3fb611b46bad41818db3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent(typeof(Camera))] | |
public class CameraTargeting : MonoBehaviour | |
{ | |
// Which layers targeting ray must hit (-1 = everything) | |
public LayerMask targetingLayerMask = -1; | |
// Targeting ray length | |
private float targetingRayLength = 3; | |
// Camera component reference | |
private Camera cam; | |
// | |
private string info = @"Left Click - switch flashing for object under mouse cursor | |
Right Click - switch see-through mode for object under mouse cursor | |
'1' - fade in/out constant highlighting | |
'2' - turn on/off constant highlighting immediately | |
'3' - turn off all types of highlighting immediately | |
"; | |
// | |
void Awake() | |
{ | |
cam = GetComponent<Camera>(); | |
} | |
// | |
void Update() | |
{ | |
TargetingRaycast(); | |
} | |
// | |
public void TargetingRaycast() | |
{ | |
// Current target object transform component | |
Transform targetTransform = null; | |
// If camera component is available | |
if (cam != null) | |
{ | |
RaycastHit hitInfo; | |
// Create a ray from mouse coords | |
Ray ray = cam.ScreenPointToRay(Input.mousePosition); | |
// Targeting raycast | |
if (Physics.Raycast(ray, out hitInfo, targetingRayLength, targetingLayerMask.value)) | |
{ | |
// Cache what we've hit | |
targetTransform = hitInfo.collider.transform; | |
} | |
} | |
// If we've hit an object during raycast | |
if (targetTransform != null) | |
{ | |
// And this object has HighlighterController component | |
HighlighterController hc = targetTransform.GetComponentInParent<HighlighterController>(); | |
if (hc != null) | |
{ | |
// Transfer input information to the found HighlighterController | |
if (Input.GetButtonDown("Fire1")) { hc.Fire1(); } | |
if (Input.GetButtonUp("Fire2")) { hc.Fire2(); } | |
hc.MouseOver(); | |
} | |
else { | |
hc.MouseExit(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using HighlightingSystem; | |
public class HighlighterController : MonoBehaviour | |
{ | |
public bool seeThrough = true; | |
protected bool _seeThrough = true; | |
public string interactionType = "dialog"; | |
public GameObject otherobject; | |
bool allowInteraction = true; | |
bool curtain; | |
public int dialogID; | |
bool looking; | |
public GUIStyle CenterStyle; | |
protected Highlighter h; | |
dooropenclose doorreference; | |
talk talkreference; | |
// | |
protected void Awake() | |
{ | |
h = GetComponent<Highlighter>(); | |
if (h == null) { h = gameObject.AddComponent<Highlighter>(); } | |
} | |
// | |
void OnEnable() | |
{ | |
if (seeThrough) { h.SeeThroughOn(); } | |
else { h.SeeThroughOff(); } | |
} | |
// | |
protected void Start() { } | |
// | |
protected void Update() | |
{ | |
if (_seeThrough != seeThrough) | |
{ | |
_seeThrough = seeThrough; | |
if (_seeThrough) { h.SeeThroughOn(); } | |
else { h.SeeThroughOff(); } | |
} | |
looking = false; | |
} | |
public void MouseExit(){ | |
allowInteraction = true; | |
} | |
// | |
public void MouseOver() | |
{ | |
// Highlight object for one frame in case MouseOver event has arrived | |
h.On(Color.red); | |
looking = true; | |
if (vp_Input.GetButton ("Interact")) { | |
if (allowInteraction){ | |
allowInteraction = false; | |
switch (interactionType) { | |
case "doorLock": | |
dooropenclose door = otherobject.GetComponent<dooropenclose>(); | |
door.lockit(); | |
if (!door.open){ | |
talkreference = gameObject.AddComponent<talk>(); | |
talkreference.customStuff(dialogID); | |
} | |
break; | |
case "volya": | |
talkreference = gameObject.AddComponent<talk>(); | |
talkreference.customStuff(dialogID); | |
otherobject.SetActive(true); | |
break; | |
default: | |
case "dialog": | |
talkreference = gameObject.AddComponent<talk>(); | |
talkreference.customStuff(dialogID); | |
break; | |
case "doorInteract": | |
if (!doorreference){ | |
doorreference = gameObject.AddComponent<dooropenclose>(); | |
} | |
doorreference.interact(); | |
break; | |
case "curtainInteract": | |
if (!gameObject.GetComponent<Animation>().isPlaying) | |
{ | |
if (curtain){ | |
gameObject.GetComponent<Animation>().Play("close"); | |
curtain = false; | |
} | |
else{ | |
gameObject.GetComponent<Animation>().Play("open"); | |
curtain = true; | |
} | |
} | |
break; | |
} | |
} | |
} | |
} | |
void OnTriggerEnter(Collider other) { | |
switch (interactionType) { | |
case "curtainInteract": | |
if (curtain) | |
gameObject.GetComponent<Animation>().Play("close"); | |
curtain = false; | |
break; | |
} | |
} | |
protected void OnGUI() | |
{ | |
if (looking) | |
{ | |
GUI.color = new Color(1, 1, 1, 1); | |
GUI.Label(new Rect((Screen.width / 2) - 200, 140, 400, 20), "Press E to interact", CenterStyle); | |
} | |
} | |
// | |
public void Fire1() | |
{ | |
// Switch flashing | |
h.FlashingSwitch(); | |
} | |
// | |
public void Fire2() | |
{ | |
// Stop flashing | |
h.SeeThroughSwitch(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment