Mouse controled 3rd person aim

hi there…

making 3rd person death match game, and i’m working on the controls right now.

As you can see on the image there are a Player and a box. A box is actually a crosshair primitive attached to the Player, and guns are looking at it.

Now i’m rotating a starship using mouth (left-right)

But i want crosshair to be attached to a mouse cursor and the Player to follow it.

Additionally crosshair should be moving only in two dimensions (x,z)

So… help needed…

Thanks!

Help needed with which part? You need to break the problem down into its constituent parts and solve each part, then you can come back with specific questions when you get stuck. Here’s a starting point for a possible breakdown:

  • Convert mouse position to screen- and world-coordinate positions
  • Draw crosshair at mouse screen-position
  • Adjust player orientation towards mouse world-position

You are right,
actually i don’t know how to convert mouse position to game coordinates. Additionally Y should be static.
Some code examples would be nice.

Perhaps i should use Input.mousePosition, convert it somehow to var crossHairPosition: Vector3 and crosshair object should have code like transform.position(crossHairPosition)

Converting from mouse position to crosshair position is trivial if you’ll be using a GUI.something call (e.g. GUI.label) to draw your crosshair – just invert the y-coordinate from Input.mousePosition:

Vector2 pos = Input.mousePosition;
pos.y = Screen.height - pos.y;

If you want to use a GUITexture, you’ll need viewport coordinates I think. You can get those either by scaling mousePosition by Screen.width/height, or using ScreenToViewportPoint().

The Camera class also has ScreenToWorldPoint() and WorldToScreenPoint() functions which you can use to convert between mouse position and world coordinates. You may need that when you come to figuring out where to point the player so it follows the cross-hair position.

thanks a lot, ScreenToWorldPoint()is just what I need!