Created
April 27, 2017 14:07
-
-
Save adarapata/10b09144fa63a2965a7639b7bcf3d418 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[RequireComponent(typeof(Camera))] | |
public class ClickRayCastObservable : MonoBehaviour | |
{ | |
public LayerMask targetLayer; | |
public float distance; | |
public Mouse moueButton; | |
private Subject<RaycastHit> raycastStream = new Subject<RaycastHit>(); | |
public IObservable<RaycastHit> ClickRaycastObservable | |
{ | |
get { return raycastStream.AsObservable(); } | |
} | |
private Subject<Unit> raycastFailedStream = new Subject<Unit>(); | |
public IObservable<Unit> RaycastFailedAsObservable | |
{ | |
get { return raycastFailedStream.AsObservable(); } | |
} | |
void Start() | |
{ | |
var selfCamera = GetComponent<Camera>(); | |
this.UpdateAsObservable() | |
.Where(_ => Input.GetMouseButtonDown((int)moueButton)) | |
.Select(_ => selfCamera.ScreenPointToRay(Input.mousePosition)) | |
.Subscribe(ray => { | |
RaycastHit hit; | |
if(Physics.Raycast(ray, out hit, distance, targetLayer.value)) | |
{ | |
raycastStream.OnNext(hit); | |
} | |
else | |
{ | |
raycastFailedStream.OnNext(Unit.Default); | |
} | |
}); | |
} | |
} | |
public enum Mouse | |
{ | |
Left = 0, | |
Right = 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment