I am trying to do a Raycast in the direction my enemy unit is facing. It starts off stand still and is set to vector3.zero, so it looks up the Z axis (top down game). The code I have right now is the following:
Raycasthit hit;
ray = new Ray(transform.position, inputRotation); //transform.forward needs to be changed to direction.
if (Physics.Raycast(ray, out hit, 7f))
{
if(hit.collider)
{
inputMovement = hit.transform.position - transform.position;
}
I have a ton of units around each other and none of them move. There’s no way a ray isn’t hitting, so there must be something else going on. Can anyone see anything I might’ve missed? I’m pretty sure my inputRotation is giving me the correct #'s since it works for movement. This is practically straight ripped from the Unity Resource section, so I don’t know why it’s messing up…
I think you are just looking for
var ray = new Ray (transform.position, transform.forward);
are you sure something is within 7 units that is not in layer 2?
Could I suggest a new path to follow? add a game object and a sphere collider as a trigger with a 7 unit radius. Then use the OnTriggerEnter, OnTriggerExit and OnTriggerStay to check to see first, if something is in front of the unit, if that unit is friendly or not and if it can see that unity.
use Unity - Scripting API: Vector3.Dot to determine if something is in front or behind. numbers above zero are in front. the closer you force it to 1 the narrower the cone. (simulating forward sight.)
I tried transform.forward before (tried again as well). I didn’t realize transform.forward actually meant in front of them. When I looked up vector3.forward, it gave 0,0,1, so I thought it meant up the Z axis, no matter which direction you were facing.
However, after plugging that in, I’m getting bugs that I was getting when I tried it before.
The units (which are all of the same prefab on the same layer) will walk towards a unit in front of them, which is good. They then pass the unit, go a little ways, then turn around and head back towards it. I assume if transform.forward is supposed to mean in front of them, this shouldn’t happen.
As for using collider triggers, I can never seem to get them to work so I try to avoid them lol. A ray seems better anyway since having a sphere collider on every prefab might be more performance draining than calling a ray only when I need to.
If your objects look along the +z axis in local space, then transform.forward will indeed be the forward vector in world space. (If your agents aren’t behaving correctly or the raycast isn’t working as you expect, the cause of the problem is likely elsewhere.)
God I figured it out. The fix was so simple, I couldn’t believe I hadn’t tried it before now…
There was an optical illusion going on with the models. I thought they were doing their raycast in the forward direction and the backwards direction, but really it was just backwards. All I had to do was replace transform.forward with -transform.forward and it worked flawlessly -.-. Doh!
Thanks for the help though!