Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SphericalRaycaster : MonoBehaviour {
- public int motorCount = 6;
- public float motorRayDistance = 5f;
- public float rayHeightFromBase = 0.1f;
- public LayerMask rayLayers;
- Transform bTransform;
- float[] motorDistances;
- /*
- The motorDistances array contains each motors distance from a wall
- in it's direction. Index 0 is straight forward from the player, increasing
- the index gets the next motor/ray in clockwise direction. A value of
- 'float.MaxValue' means it did not hit a wall (Maybe use -1 instead).
- */
- void Awake()
- {
- this.bTransform = GetComponent<Transform>();
- this.motorDistances = new float[this.motorCount];
- }
- void GetMotorDistances()
- {
- if(this.motorDistances.Length != this.motorCount)
- {
- this.motorDistances = new float[this.motorCount];
- }
- RaycastHit hit;
- for(int i=0; i<this.motorCount; i++)
- {
- float progress = ((float)i) / this.motorCount;
- float angle = progress * Mathf.PI * 2;
- Vector3 origin = this.bTransform.position + (Vector3.up * this.rayHeightFromBase);
- Vector3 direction = this.bTransform.rotation * new Vector3(Mathf.Sin(angle), 0f, Mathf.Cos(angle));
- Ray ray = new Ray(origin, direction);
- Color debugCol = Color.red;
- if(Physics.Raycast(ray, out hit, this.motorRayDistance, this.rayLayers))
- {
- this.motorDistances[i] = hit.distance;
- debugCol = Color.green;
- }
- else
- {
- this.motorDistances[i] = float.MaxValue;
- }
- Debug.DrawRay(ray.origin, ray.direction * this.motorRayDistance, debugCol);
- }
- }
- void Update ()
- {
- this.GetMotorDistances();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement