how to adjust the barrel of a gun?

Hello to all forum, I’m new here more like to say that I really enjoyed the forum and he always helps me.

Well my doubts:
I made a game of first-person shooter using Raycaster, and I used a GUI as a viewfinder, I rotate the “Empty Object” to set the crosshairs in the center of the screen, and she hit the point perfectly, however it only happens to a certain distance, and this makes the gun is not accurate (eh have not wHAT she is now more like an exaggeration) I wonder if you have any script or method so that the sight is constant for all distances.

Thank you for your attention
Eskiel Santana.

Sorry for any grammar errors or violations on the forum.

To make it 100% accurate, you would:

  1. Raycast
  2. Get the point (in world coordinates) where the raycast hit.
  3. Convert that point to screen coordinates
  4. Display your GUI at those coordinates

Dman first of all thanks for helping me, I understood what he meant but I can not get past this idea to the script could help me?

Something along the lines of

var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit) {
    DrawGUIReticle (camera.main.WorldToScreenPoint (hit.point));
}

Eu coloco esse script no GUI?

e o script não esta reconhecendo o codigo “DrawGUIReticle”

You place it somewhere on your gun, most likely in an Update function.

DrawGUIReticle is a placeholder. I don’t know how you are drawing your GUI.

I put on the update of my gun, and gave the same error in DrawGUIReticle, I can not solve.

and the script I’m currently using for my aim is this:

As I said, DrawGUIReticle is a placeholder. Its not an actual function unless you define it.

I just threw this together, but it should work.

var mira : Texture2D;
var posicao : Rect;

function Start()
{
    Screen.showCursor = false;
    posicao = Rect(( Screen.width - mira.width )/2,(Screen.height-mira.height)/2,mira.width,mira.height);
}

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (transform.position, -Vector3.up, hit) {
        DrawGUIReticle (camera.main.WorldToScreenPoint (hit.point));
    } else {
        DrawGUIReticle (Vector3 (-100, -100, 0)); // No reticle
    }
}

function DrawGUIReticle (point : Vector3) {
    point.y = Screen.height-point.y;

    posicao.x = point.x-mira.width;
    posicao.y = point.y-mira.height;
}

function OnGUI()
{
    GUI.DrawTexture(posicao,mira);
}

tem um erro nessa linha:

function DrawGUIReticle (Vector3 point)

unexpected token: point.

Edited my post. Its a side affect of using C# when working and JavaScript for examples :stuck_out_tongue:

Thanks … apparently it worked! now I just turn my “Empty Object” to focus the crosshairs?

This method moves the sight to wherever the gun is pointing. If you want to move the gun to wherever the sight is pointing without moving the sight, it would be more like this:

function Update () {
    var hit : RaycastHit;
    if (Physics.Raycast (camera.main.ScreenPointToRay (Screen.height/2, Screen.width/2, 0), hit) {
        transform.LookAt (hit.point);
    }
}

have an error in this line:

if (Physics.Raycast (camera.main.ScreenPointToRay (screen.height / 2, screen.width / 2, 0), hit))

error:
The best overload for the method ‘UnityEngine.Camera.ScreenPointToRay (UnityEngine.Vector3)’ is not compatible with the argument list ‘(int, int, int)’.

You need to change it to a Vector3 instead of (int, int, int) as the error is telling you.

You shouldn’t take my code word for word. Theres a reason I add “it would be more like this.” I don’t put too much effort in to keep my code error free on the forums.

Besides, its a good exercise for you to fix the code :stuck_out_tongue: