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.
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.
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;
}