- Home /
RayCast to all directions
Hi,
i am having issue with raycasting. I would need object to do a raycast to all directions from its position and then check the rayhit vector so i could invert the vector for the object so it moves away from the rayhit. is this somehow possible ? Something like checksphere, but getting also the data from the hit, mostly the vector between the object and the rayhit.
I did some solution with raycast into 5 directions combined with checksphere, but its too heavy i guess, its almost 500 lines and i dont think its clean solution, when i am sure there is better way.
Thanks for any advise.
Answer by Henrique Vilela · May 04, 2011 at 01:37 AM
You can use Physics.OverlapSphere to get all the objects that are on the desired radius, than Transform.TransformPoint on the enemy position to get the direction vector.
Answer by zmar0519 · Apr 26, 2011 at 12:30 AM
look here for a raycasting version of a collider, not sure if it's exactly what your looking for, but it wouldn't be to hard to modify it to have hit raycast hit vars. Good luke, Luke!!! :)
Also, the code is listed below:
#pragma strict
var layerMask : LayerMask; //make sure we aren't in this layer var skinWidth : float = 0.1; //probably doesn't need to be changed private var minimumExtent : float; private var partialExtent : float; private var sqrMinimumExtent : float; private var previousPosition : Vector3; private var myRigidbody : Rigidbody; //initialize values function Awake() { myRigidbody = rigidbody; previousPosition = myRigidbody.position; minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z); partialExtent = minimumExtent*(1.0 - skinWidth); sqrMinimumExtent = minimumExtent*minimumExtent; }
function FixedUpdate() { //have we moved more than our minimum extent? var movementThisStep : Vector3 = myRigidbody.position - previousPosition; var movementSqrMagnitude : float = movementThisStep.sqrMagnitude; if (movementSqrMagnitude > sqrMinimumExtent) { var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude); var hitInfo : RaycastHit; //check for obstructions we might have missed if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value)) myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent; } previousPosition = myRigidbody.position; }
Answer by zmar0519 · May 04, 2011 at 12:43 AM
You could use Vector3.distance to calculate if something came close, not sure how to tell which direction it's coming from, though.
Answer by Richard J. Hansen · May 04, 2011 at 03:25 AM
Could you you just use a collision sphere and then just the direction to/away from the contact point?
Your answer
Follow this Question
Related Questions
Magic Leap Raycasting - Direction 2 Answers
Using raycast and collider to increase int 1 Answer
Raycast not working 2 Answers
RaycastHit direction and start position 0 Answers
Raycasting Through Terrain 1 Answer