- Home /
Orthographic TopDown game Shooting Direction
Greeting,
I am having problem with calculation the right shooting direction in my orthographic topdown shooter. As I don't use true TopDown(I don't look on player right on top, but instead camera has about 45 degree angle) there is some distortions with correct shooting direction.
Take a look at screenshots(my win 8 doesn't include cursor in screenshots, so I painted it by hand)
when I look down or up, cursor, player direction and shooting direction ling up correctly, but when I look left or right distortion comes up:
because I use the code that rotates player to where mouse position is on the floor, player turns to that point, but gun is higher and the camera angle makes it appear that he is aiming at totally different direction.
Can you help me how to solve this problem, I want the player to always turn and aim at mouse position.
My code :
void Update()
{
//Do raycast from the mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100, floorMask))
{
//...and if it hit's something then update the look position
//we do it this way in case the mouse didn't hit something, our character will have something to look at
lookPos = hit.point;
}
//Find the direction we want to look at
lookDir = lookPos - transform.position;
//kill the y value
lookDir.y = 0;
transform.LookAt(transform.position + lookDir, Vector3.up);
}
Answer by SilentSin · Dec 23, 2015 at 07:01 AM
Calculate the gun height and move the ray origin down by that amount before casting.
Your answer
Follow this Question
Related Questions
2D TOP DOWN - W A S D as direction 2 Answers
Instantiating a prefab/bullet and sending it to my mouse position?? 0 Answers
can't get my floats to update in the animator 2 Answers
Setting rotation angle of a bullet fired towards the mouse 1 Answer
I am having problems with making my character face the movement direction after stopping. 1 Answer