EDIT: Alright, I've figured out what's wrong. I'm calculating the direction vector properly with the below code:

//Get our ship position in screen space
Vector3 shipPos = Camera.main.WorldToScreenPoint(GameObject.FindGameObjectWithTag("Player").transform.position);

//Get the direction vector from our ship to our mouse
Vector3 direction = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0) - new Vector3(shipPos.x, shipPos.y, 0);

//Give the clone script our direction vector.
direction = Camera.main.transform.TransformDirection(direction);

This gives out the proper direction vector relative to our camera (I've tested it with a Debug.DrawRay()). The problem is, in order to get the bullets to orbit around the planet I'm using the below code:

//Rotate our bullet around the planet, our axis of rotation is the vector perpendicular to targetDir
transform.RotateAround(Ship_Motor.instance.planet.transform.position, new Vector3(-targetDir.y, targetDir.x, 0f), -speed * Time.deltaTime);

My calculation for an axis based on our direction works when the ship is right side up. However when I move it around to be at a different angle that's when things go wrong. So it seems as if my code for calculating the axis to rotate the bullet around is what I'm doing wrong. Does anyone have suggestions how I could fix it so that transform.RotateAround() would make the bullet start orbiting the planet in the direction it was fired off in?


I'm making a top down shooter, however gravity is a bit weird in this game. At certain times the player may be upside down compared to global vectors, or at other odd angles.

Basically what I'm looking to do is get the player position in screen space, get a direction vector from that to the mouse so I know what direction the player should shoot, then translate that back to a 3D vector and have the player shoot that direction relative to the camera. Basically if the mouse is to the right of the player on screen, I want the player to shoot right.

The only problem with this is that when I translate the vector back to 3D, it's not always relative to the camera. And transform.transformdirection() doesn't appear to help.

Is there a way I could get a local vector from the camera and get the relative vector for the player? e.g. get the camera's local right vector, then figure out how that is for the player (possibly forward for the player or some such.)

I'm writing this in a hurry right now, I can add more details and exact code later if anyone requests it.

  • \$\begingroup\$ Take a vector that perpendicular to the camera heading, rotate it to point towards the mouse? \$\endgroup\$ – MichaelHouse Jul 18 '13 at 16:47
  • \$\begingroup\$ Well you can get the world position of a click using a built in camera function: docs.unity3d.com/Documentation/ScriptReference/… after than I would just use Vector3.up or the inverse your cameras rotation quaternion for the up vector to keep your character properly oriented. \$\endgroup\$ – Benjamin Danger Johnson Jul 18 '13 at 17:08
  • \$\begingroup\$ Byte: How could I go about doing that? I know how to get a vector perpendicular to the camera forward, how could I then get it to point towards the mouse? \$\endgroup\$ – Timothy Williams Jul 18 '13 at 17:34
  • \$\begingroup\$ Ben: I already use screentoworldpoint, but it's causing the errors above where it doesn't shoot properly. \$\endgroup\$ – Timothy Williams Jul 18 '13 at 17:34
  • 1
    \$\begingroup\$ A screenshot might be helpful (perhaps with some vectors drawn in with annotations). Also, remember to use the @ before a name to get someone's attention @TimothyWilliams. \$\endgroup\$ – MichaelHouse Jul 18 '13 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.