Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mouse aim in orthographic view

Discussion in 'Scripting' started by markblank05, Apr 12, 2015.

  1. markblank05

    markblank05

    Joined:
    Apr 2, 2015
    Posts:
    29
    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.

    2015-04-11_21-13-59.jpg


    Code (CSharp):
    1. void Shoot ()
    2.     {
    3.         timer = 0f;
    4.         gunAudio.Play ();
    5.         gunLight.enabled = true;
    6.         gunParticles.Stop ();
    7.         gunParticles.Play ();
    8.         gunLine.enabled = true;
    9.         gunLine.SetPosition (0, transform.position);
    10.         shootRay.origin = transform.position;
    11.         shootRay.direction = transform.forward;
    12.         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
    13.         {
    14.             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
    15.             if(enemyHealth != null)
    16.             {
    17.                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
    18.             }
    19.             gunLine.SetPosition (1, shootHit.point);
    20.         }
    21.         else
    22.         {
    23.             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
    24.         }
    25.     }
    Code (CSharp):
    1. void Turning()
    2.     {
    3.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.         RaycastHit floorHit;
    5.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
    6.         {
    7.             Vector3 playerToMouse = floorHit.point - transform.position;
    8.             playerToMouse.y = 0f;
    9.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    10.             playerRigidbody.MoveRotation(newRotation);
    11.         }
    12.     }
     
  2. Pablo-R

    Pablo-R

    Joined:
    Mar 17, 2015
    Posts:
    2
    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.
     
    Last edited: Apr 13, 2015
  3. markblank05

    markblank05

    Joined:
    Apr 2, 2015
    Posts:
    29
    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

    Code (CSharp):
    1. Vector3 offset = transform.position - gunPosition.position;
    2. offset.y = 0f;
    3.  
    4. Quaternion newRotation = Quaternion.LookRotation(playerToMouse + offset);
    5. 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.
     
  4. Pablo-R

    Pablo-R

    Joined:
    Mar 17, 2015
    Posts:
    2
    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.

    Code (CSharp):
    1.     void Turning()
    2.     {
    3.         Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit floorHit;
    5.  
    6.         if (Physics.Raycast(camRay, out floorHit, camRayLenght, floorMask))
    7.         {
    8.             Vector3 playerToMouse = floorHit.point - transform.position;
    9.             playerToMouse.y = 0f;
    10.  
    11.  
    12.             if (playerToMouse.x < 0)
    13.             {
    14.                 playerToMouse.z -= 1f;
    15.             }
    16.             else
    17.             {
    18.                 playerToMouse.z -= 0.3f;
    19.             }
    20.  
    21.             if (playerToMouse.z > 0)
    22.             {
    23.                 playerToMouse.x -= 0.2f;
    24.             }
    25.             else
    26.             {
    27.                 playerToMouse.x += 0.3f;
    28.             }
    29.  
    30.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    31.             playerRigidbody.MoveRotation(newRotation);
    32.         }
    33.     }

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

    Regards!