Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to make my "ray" always hit center of the screen?

Discussion in 'Scripting' started by ShimmyDev, Aug 20, 2020.

  1. ShimmyDev

    ShimmyDev

    Joined:
    Nov 21, 2016
    Posts:
    11
    Hey guys. I've got an issue. I don't know how to fix this.

    So I want my _laserPrefab to always hit the middle of the screen, but for now, it looks like this:
    For far object's it's almost in the center



    ----------
    But for the close ones it looks terrible:



    ----------


    My code:


    Code (CSharp):
    1. RaycastHit hit;
    2.             if (Physics.Raycast(_camera.transform.position, _camera.transform.forward, out hit, range))
    3.             {
    4.                 TargetController target = hit.transform.GetComponent<TargetController>();
    5.  
    6.                 if (target != null)
    7.                 {
    8.                     target.TakeDamage(damage);
    9.                 }
    10.  
    11.             Vector3 rot = _camera.transform.rotation.eulerAngles;
    12.  
    13.             rot = new Vector3(rot.x, rot.y, rot.z);
    14.  
    15.             var midPointRay = hit.distance / 0.5f;
    16.  
    17.             GameObject railRay = Instantiate(_railPrefab, transform.position + new Vector3(0, 0, 0), Quaternion.Euler(rot));
    18.  
    19.             railRay.transform.localScale = new Vector3(railRay.transform.localScale.x, railRay.transform.localScale.y, midPointRay);
    20.  
    21.             Destroy(railRay, 2f);
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,214
    You'll most likely want to use "hit.point" to determine where your laser should be aiming. Assuming you've centered the beam in its parent properly, you could probably also just use Transform.LookAt(hit.point) to get it to rotate and aim at the exact point it hits the target.
     
  3. RakNet

    RakNet

    Joined:
    Oct 9, 2013
    Posts:
    315
    What you are trying to do is more difficult than you probably expect.

    Raycast from the center of the screen to find out the point that was shot.
    Base the damage done based on the raycast from the center of the screen
    Instantiate the rail effect from the tip of the gun, point it at the point that was shot using LookAt(hit.point) as the prior commented suggested.
    It's going to look strange that the rail effect won't align with the gun itself. You either need a rail that is visually disconnected from the gun, such as a projectile that moves over time, or move the gun to the bottom center of the screen
     
    Zer0Cool likes this.
  4. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    You should shoot your ray from the mid of your screen position like this:
    Code (CSharp):
    1. Ray ray = _camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    2. RaycastHit hit;
    3. if (Physics.Raycast(ray, out hit, range)) {
    4.   ...
    5.   ...  
    6. }
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,214
    Why should he do that, specifically? He's already performing the raycast from the camera's position, directly forward. He's not trying to raycast to the mouse position or anything. How would the approach you're suggestion change anything?

    The raycast isn't the problem. The problem is what he's doing with the result of the raycast.
     
    PraetorBlue likes this.