- Home /
Question by
markblank05 · Apr 13, 2015 at 07:50 PM ·
raycast
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);
}
}
2015-04-11-21-13-59.jpg
(197.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613192904im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Glass break effect 2 Answers
Help for raycasting 2d with multiple layers 1 Answer
Raycast with a mouse position! 1 Answer
problem with targeting system with raycast; 1 Answer
Is there a way to get mouse position in 3D space at a given y-value? 2 Answers