Topdown to 3d

So I’ve been following the Evac City tutorial for the top down game, and its been a big help.

However I want my game to have a slight 3d perspective, which it does not cover. My issue lies with the camera angles matching up with the crosshair on the screen.

In topdown mode, the crosshair is drawn on the mouse position on the screen, and the player rotates to that position. Since it’s exactly at the right angle, staring down at the player it works. However,if the camera is rotated back about the X axis (say 60 degrees instead of 90), the plane of the screen/cursor position is now at an angle to the plane of the player, thus making the shots not line up.

I’m trying to figure out a mathematical way to adjust for this but I’m coming up short. Then of course I have to figure out how to code it in.

Any one good at math out there could help me out with this problem? :slight_smile:

I’m not sure about the tutorial in question, as I’ve only just started with unity my self.

Would somthing like this work?

Vector3 MoveTo = Camera.main.ScreenToWorldPoint(Input.mousePosition);

MoveTo.y = player1.transform.position.y; // assuming the game area is flat.
Vector3 Direction = MoveTo - player1.position;

if(Vector3.Distance(MoveTo, player.transform.position) > 0.1f){
 player1.transform.rotation = Quaternion.Slerp (player1.transform.rotation,
 Quaternion.LookRotation(Direction), rotationSpeed * Time.deltaTime); 
 transform.eulerAngles = new Vector3(0, player1.transform.eulerAngles.y, 0); 
 Direction.Normalize();
 player1.GetComponent<CharacterController>().SimpleMove(Direction * moveSpeed);
}

I’m using something similar but in othorgraphic mode from a 45 degree X Y rotation and the character movement looks ok but the characters not doing any shooting to I’m not sure if it exect enough to line up shots, etc.

Also I attached the code to my character and called it by passing in the MoveTo from the main game… I just bunched it all together here for clarity.

//EDIT: I may not have understood the question… is the rotation ok but the shots are wonky? or does the character always shoot directly ahead in the direction he rotated to?

Character shoots in the direction he’s rotated to. At 30 degree angle its not that noticeable. When character is facing forwards or back it lines up perfectly, anywhere in between is variable, but at most about 10 degrees off.

I think I might almost be able to cover it up with a cone of fire randomization but it would be nice to get it right.

You can use Camera.ScreenPointToRay to get a ray extending from the camera, through the crosshair to the correct position on the ground. A good way to get the coordinate on the ground would be to use a Plane to represent the plane of the ground and then use Plane.Raycast to find the point where the ray intersects the ground. You then just need to aim the character at this intersection point.