Mouse Position to Screen Coordinate Problems

Hi all, I’m wondering if you could help me with a small issue I’m having.

I’m developing a sailing game for a project in College. It’s going to be a game for the Android, eventually, but for now, I’m just developing it with PC controls for convenience. Trying to simulate a touch and swipe with the mouse is my current issue.

Basically, my boat’s going to have broadside cannons, but the camera is fixed meaning that the boat’s rotation is independent from the camera, so the direction of the swipe will determine which side the cannons are fired out of. That part is figured out though, I have a good idea on what I’m doing for that. My current problem is the actual inputs themselves.

When I click my mouse, it’s in a fixed location, which could skew the direction result, I want it to update with my sailboat’s movement, I also want to draw an arrow from the clicked position (in screen coordinates), to where the mouse currently is (in screen coordinates), but it never appears on screen, in fact, it appears way above the camera.

Does this make sense? I have a feeling I’m not getting my exact problems across.

My code is as follows (in the update function):

// Get if the mouse is clicked and held
if ( Input.GetMouseButtonDown( 0 ) )
{
	_mouseClickPosition = Input.mousePosition + gameObject.transform.position;

	      
    _isDragging = true;
}

// if you want to input an aiming arrow, do it here
if ( _isDragging )
{
    Debug.DrawLine(_mouseClickPosition, Input.mousePosition, Color.white);
}
             

// mouse released
if ( Input.GetMouseButtonUp( 0 ) && _isDragging )
{
    _mouseReleasePosition = Input.mousePosition;
	

	_isDragging = false;
}

Edit

I should probably mention, what I’m aiming for is something like Angry Birds’ slingshot mechanic (though it only depends on the rotation which isn’t limited), except it’ll be moving along with the player.

Edit 2

I utilized my fantastic paint skills to try and get a screenshot of my current problem

alt text

Okay, I got it to mostly work with using ScreenToWorldPoint (I had tried it previously, but it seems the line was being rendered too high for me to actually see).

Here’s the updated code:

// Get if the mouse is clicked and held
if (Input.GetMouseButtonDown(0))
{
    _mouseClickPosition = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));

    _isDragging = true;
}

// if you want to input an aiming arrow, do it here
if (_isDragging)
{
    Debug.DrawLine(
        _mouseClickPosition,
        Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, 20 ) ),
        Color.white );

}


// mouse released
if (Input.GetMouseButtonUp(0) && _isDragging)
{
    _mouseReleasePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));

    _isDragging = false;
}

There’s still the problem of the origin point not moving with the player, so the beginning point of the line crawls off screen. Is there a way for it to keep up with the player? (The camera is above the player, looking down at a slight angle, while the player moves along the X and Z axis’, if I add the gameObject.transform.position to the _mouseClickPosition, it’ll have some strange behaviour of starting the line at different places).

The problem with the Debug.DrawLine is because you are telling it what camera it should map it from. If you are using only the mainCamera you can call something like:

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

But that is still going to come from the camera. More than likely you want a collision layer below your ships. A large box collider then you can raycast from the camera to that point to figure out where it hit in your game world. Something like:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, 1000, out hit, layerMaskOfItemsYouWantToRaycastAgainst))
{
      Vector3 groundPointWhereWeHit = hit.point;
}