Mouse aim in orthographic view

How can I change the gun accuracy with mouse, the way the game is design is to rotate the player with mouse position, but the aim are a little off because there are some offset from player position and gun position. how can i make the ray and line renderer to aim/cast the ray where my mouse is.

void Shoot ()
    {
        timer = 0f;
        gunAudio.Play ();
        gunLight.enabled = true;
        gunParticles.Stop ();
        gunParticles.Play ();
        gunLine.enabled = true;
        gunLine.SetPosition (0, transform.position);
        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;
        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
            if(enemyHealth != null)
            {
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }
            gunLine.SetPosition (1, shootHit.point);
        }
        else
        {
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }
void Turning()
    {
        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit floorHit;
        if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
        {
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;
            Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation(newRotation);
        }
    }

Hi,

I’ve been looking for a solution to this and playing with the raycast all day. i have yet to discover how to compensate for that offset, will let you know if i can make it work.

If anyone has figured this out already please help us.

Thanks!

Hi,

Since the gun is firing a straight shot forward from the gun end, all you need to do is to tweak the character rotation to match exactly where the shot is going.
Something like this in the characterController script, play a little with it… these values are for another character I am working in.

After line 8 in the turning method add:
if (playerToMouse.z > 0)
{
playerToMouse.x -= 0.2f;
}
else
{
playerToMouse.x += 0.2f;
}

add a similar if structure for the X axis
if (playerToMouse.x > 0)
{
playerToMouse.y -= somefloatvalue;
}
else
{
playerToMouse.y += somefloatvalue;
}

Lemme know if it works for you.

Thanks,
Pablo.

thanks, i try but it didnt work. here is my solution, i get the position of gun, then subtract it to the position of my player, then add it in the Quaternion rotation

Vector3 offset = transform.position - gunPosition.position;
offset.y = 0f;

Quaternion newRotation = Quaternion.LookRotation(playerToMouse + offset);
playerRigidbody.MoveRotation(newRotation);

it will work on top down view, but in my current camera with 30 degree, it will look like missing the mouse when shooting left or right. but working fine in the 90 degree.

Ok, i went back to the nightmares tutorial and pasted my code, it fixed the problem. Give it one more try, it should work for you also.

    void Turning()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorHit;

        if (Physics.Raycast(camRay, out floorHit, camRayLenght, floorMask))
        {
            Vector3 playerToMouse = floorHit.point - transform.position;
            playerToMouse.y = 0f;


            if (playerToMouse.x < 0)
            {
                playerToMouse.z -= 1f;
            }
            else
            {
                playerToMouse.z -= 0.3f;
            }

            if (playerToMouse.z > 0)
            {
                playerToMouse.x -= 0.2f;
            }
            else
            {
                playerToMouse.x += 0.3f;
            }

            Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
            playerRigidbody.MoveRotation(newRotation);
        }
    }

My camera rotation is not exactly like yours so you need to try new values to suit your camera position.

Regards!