Using Player Rotation Quaternion to Make a Directional Vector, Help!

Updated on November 19, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on November 19, 2017

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: 

  1. Transform firePoint;
  2. void Awake()
  3. {
  4. firePoint = transform.Find("FirePoint");
  5. }
  6. void Update()
  7. {
  8. Quaternion rotation = PlayerController.playerRotation;
  9. Vector3 direction = rotation * Vector3.forward;
  10. RaycastHit2D hit = Physics2D.Raycast(firePoint.position, direction, 100);
  11. Debug.DrawLine(firePoint.position, direction);
  12. }

The vector “direction” is always in a funny place and not in front of my player as it should be.

Any help is appreciated. Thanks so much! 🙂

  • Liked by