# Raycast attracted to scene origin (0,0,0)

I’m experimenting with a top-down 2D shooter gameplay. The player character aims at the cursor and fires high velocity bullets. From one frame to the other the bullets were skipping over my enemies so I decided to use raycast instead of colliders.

I thought what I had was working fine but I realised that enemies were dying no where near my bullets and upon further inspection I realised the rays were stretching to the world origin, which makes the bullet effect a wide margin. It was difficult to get a quality screenshot but you can see what I mean here.

I know this has something to do with the difference between points and directions, but I don’t rightly understand and I’ve tried all kinds of stuff to figure it out. Even trying linecast instead was producing strange results.

Here is all the relevant code.

``````public class Bullet : MonoBehaviour {

public Vector3 speed = new Vector3(0f,1f,0f);

void FixedUpdate ()
{
Vector3 nextPos = transform.position + speed;
RaycastHit2D hit = Physics2D.Raycast(transform.position, (nextPos.normalized));
Debug.DrawRay(transform.position, nextPos.normalized);
}
}
``````

Currently I have it normalised because I read that was the difference between a point and a direction but again I have no understanding of what’s happening, and clearly it didn’t work. If it’s not clear, what I want to happen is for the raycast to start at the bullet and trail behind it a bit.

You want to generate a ray from your current position in the direction you are moving. To get that direction, you have to subtract your current position from your next one.

``````Vector3 nextPos = transform.position + speed;
Vector3 direction = nextPos - transform.position;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction.normalized);
``````

You are currently using your objects `nextPosition` as the direction. When you do that, that position is treated as a vector starting in the coordinate system origin (0,0,0) going towards that position. When you normalize it, it still starts in the same place and goes towards the same direction.