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.