Search Unity

Crosshair Problem.

Discussion in 'Scripting' started by Gorlog, Nov 3, 2016.

  1. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    Hello my problem is player not shooting mouse cursor i want to make crosshair i want to shoot crosshair but not working i try diffent thing but i cant achive it. i follow unity3d tutorial.

    Problem Video




    Turning Code

    Code (CSharp):
    1.    
    2. void Turning()
    3.     {
    4.         Ray CamRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.         RaycastHit floorHit;
    6.  
    7.         if(Physics.Raycast(CamRay, out floorHit,camRayLength,floorMask))
    8.         {
    9.             Vector3 playerToMouse = floorHit.point - transform.position;
    10.             playerToMouse.y = 0f;
    11.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    12.             playerRigidbody.MoveRotation(newRotation);
    13.            
    14.            
    15.         }
    16.  
    17.     }
    Shooting Code

    Code (CSharp):
    1.    
    2. void Shoot()
    3.     {
    4.         timer = 0f;
    5.         gunAudio.Play();
    6.         gunLight.enabled = true;
    7.  
    8.         gunParticles.Stop();
    9.         gunParticles.Play();
    10.  
    11.         gunLine.enabled = true;
    12.         gunLine.SetPosition(0, transform.position);
    13.  
    14.         shootRay.origin = transform.position;
    15.         shootRay.direction = transform.forward;
    16.  
    17.         if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
    18.         {
    19.             EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
    20.             if (enemyHealth != null)
    21.             {
    22.                 enemyHealth.TakeDamage(damagePerShot, shootHit.point);
    23.             }
    24.             gunLine.SetPosition(1, shootHit.point);
    25.         }
    26.        
    27.         else
    28.         {
    29.             gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
    30.         }
    31.     }

    Thanks for any advice.
     
  2. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    201
    i divide the screen 4 and position drawtexture texture and fix my problem