SHOW:
|
|
- or go back to the newest paste.
| 1 | using UnityEngine; | |
| 2 | using System.Collections; | |
| 3 | ||
| 4 | public class AimFlashlight : MonoBehaviour {
| |
| 5 | - | |
| 5 | + | |
| 6 | - | public Transform playerTransform; |
| 6 | + | public Transform cam; |
| 7 | - | public float inspectDistance; |
| 7 | + | public float rayLength = 100; |
| 8 | - | public LayerMask ignoreLayer = -1; |
| 8 | + | public LayerMask mask; |
| 9 | - | |
| 9 | + | |
| 10 | - | private Transform thisTransform; |
| 10 | + | void Update () {
|
| 11 | - | |
| 11 | + | |
| 12 | - | void Awake () {
|
| 12 | + | Ray ray = new Ray( cam.position, cam.forward ); |
| 13 | - | thisTransform = this.transform; |
| 13 | + | RaycastHit hit; |
| 14 | - | } |
| 14 | + | |
| 15 | if (Physics.Raycast(ray, out hit, rayLength, mask)) | |
| 16 | - | void Update () {
|
| 16 | + | {
|
| 17 | - | RaycastHit hit; |
| 17 | + | transform.LookAt( hit.point ); |
| 18 | - | Vector3 forward = playerTransform.TransformDirection (Vector3.forward); |
| 18 | + | Debug.DrawLine( ray.origin, hit.point, Color.red ); |
| 19 | - | Physics.Raycast(playerTransform.position, forward, out hit, ignoreLayer); |
| 19 | + | } else {
|
| 20 | - | thisTransform.LookAt(hit.point); |
| 20 | + | Debug.DrawRay( ray.origin, ray.direction*rayLength, Color.green ); |
| 21 | - | Debug.DrawRay (playerTransform.position, forward * inspectDistance, Color.red); |
| 21 | + | } |
| 22 | - | Debug.DrawLine (thisTransform.position, hit.point, Color.blue); |
| 22 | + | |
| 23 | - | } |
| 23 | + | } |
| 24 | } |