- Home /
Using Player Rotation Quaternion to Make a Directional Vector
Hey Guys,
I'm having a little trouble with my Top Down 2D game. I would like my player to shoot a RayCast2D in the direction he is facing, but creating the directional vector is causing an issue. The Ray does not shoot in the correct direction. Here is my script:
Transform firePoint;
void Awake()
{
firePoint = transform.Find("FirePoint");
}
void Update()
{
Quaternion rotation = PlayerController.playerRotation;
Vector3 direction = rotation * Vector3.forward;
RaycastHit2D hit = Physics2D.Raycast(firePoint.position, direction, 100);
Debug.DrawLine(firePoint.position, direction);
}
The vector "direction" is always in a funny place and not in front of my player as it should be. Any help is appreciated.
You're stating two different problems: creating the directional vector and positioning the directional vector. I'm guessing you've just stumbled on the ter$$anonymous$$ology in "creating the directional vector is causing an issue" and with "vector" you actually mean the ray - since a vector doesn't have a position? Is "FirePoint" at the correct position if you view it in the scene view? Also keep in $$anonymous$$d that Physics2D works with Vector2, so make sure you haven't got any bugs in that area - for example that you need to use direction = rotation * Vector3.right.
Answer by voncarp · Nov 20, 2017 at 12:11 AM
You generally just use the forward position of the transform to get the direction he is facing as such:
Vector3 direction = firepoint.forward
The forward direction will be the blue arrow on the transform