Search Unity

FPS Weapon and Aim Coding...

Discussion in 'Scripting' started by Heartless49, Feb 20, 2011.

  1. Heartless49

    Heartless49

    Joined:
    Jan 21, 2011
    Posts:
    40
    So I've got everything setup and working, but I just have a question about coding the shooting part of my shooter... I have it setup so that the bullet actually shoots, or raycasts, from the barrel of the gun, which is held in the player's right hand. It works fine, but the one thing I noticed, after adding a crosshair, is that the gun doesn't actually shoot to the center of the screen because of the fact that it's actually shooting FROM the gun itself. After reading around a bit I learned that everyone usually just raycasts from the camera... in hopes to keep realism in that aspect, I want to keep the gun setup to shoot from the actual gun, not the screen.

    My question is, would there be any way to change my setup slightly and get it to work right, if not, is there a way I can take a mixture of raycasting from the screen, AND from the barrel to find a nice medium? I was thinking I could also just change the crosshair position to be located from the gun's position versus the center of the screen, but something tells me that doing it that way might upset players, since everyone's so used to it being in the center of the screen.

    As it stands, the gun is positioned and rotated to aim in the right direction/angle, so I know that isn't the issue... unless I need to adjust it slightly to fix this, in which case, how would I find the right adjustment value to make?

    And about the code - it's really nothing complicated, just send a raycast from the character's right hand in a forward direction, then add a gui crosshair to the center of the screen... it doesn't line up. Maybe I'm just missing something here, but everywhere that I search for answers, or any problems LIKE this, just result in people being told to raycast from the camera, which I want to avoid.