Search Unity

Raycast doesn't get centered

Discussion in 'Scripting' started by ViFFeX, Mar 28, 2012.

  1. ViFFeX

    ViFFeX

    Joined:
    Mar 28, 2012
    Posts:
    4
    Hi all,

    First post here with a probably noobish question ^^

    I am following ETeeski's http://youtube.com/ETeeskiTutorials tutorials on creating an FPS in Unity.

    I have gone through episodes 1-18 three times, however, always seem to make the same mistake.

    I have created a gun in a separate 3D application because I wanted to start off with usable models. In the tutorials ETeeski just uses some boxes to make his gun, but the code should be all the same.

    Everything works, aiming down the sights/following the player/rotating into view with the camera etc.

    However, for some reason, when I am not aiming down the sights the Raycast that fires the bullet and creates the bullethole on an object in my scene, is not centered on screen. At least, that's what I think as the bulletHole object gets created off center to the right.

    When aiming it's all perfect. It lines up perfectly with the gun. The gun is centered when aiming down the sights, and the bulletholes get created on the center of where I fired. Just as soon as I am zoomed out the bullets are offset to the right. I kind of think to know why, as the gun is held at the right side of the screen, but looking at the videos ETeeski presents, he apparently hasn't got this problem and I tried to fix this issue by starting over 3 times already. -_-

    http://www.viffex.com/Alpha/WebPlayer.html <-- I think it's best described if you try it yourself ^^. Don't mind the surroundings and the gun, it's all in it's very early stages :)

    When aiming [--0--]
    When not aiming [---0] <-- If this makes any sense ^^

    Is this something I can fix in code? Or something that I have to fix in the Inspector?

    I have my gun's local coordinates at x: 0, y: 0, and z: 0.75.
     
  2. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    Without the code it is hard to find your problem, however, from the (very) quick look I took, I would ask the question of what is the origin of your raycast. If you are shooting the ray using screen space, it makes sense, as the gun isn't centered on the screen.
     
  3. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    Are you firing the ray using the gun as a starting point? This is the obvious mistake based on what you describe.

    You have to cast the ray from the center of the screen instead, the bullet should be separate and start from the gun and end at the hit point (if you want to render a bullet)

    .
     
  4. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    to answer skalev and koyima, yes, the bullet is spawn from the position of the gun. I actually do get the same "problem" in my game. I think it's a little more hidden in my game because my gun has a larger spread when not aiming. That's just the way i wanted to design this fps. To fix this problem, you have to do a little modding in code.
    You could try creating a second bullet spawn that is a child of the main camera, then when you spawn bullets, spawn the bullets at the position of the main camera spawn and the rotation of the gun spawn. That way, you'll be shooting down the center of the screen, but still get bullet spread from the gun. Then you could just make the muzzle flash spawn on the gun spawn so that it looks like the bullets are coming from the gun.
     
  5. ViFFeX

    ViFFeX

    Joined:
    Mar 28, 2012
    Posts:
    4
    Thanks all! I was thinking it was indeed due to the fact that the gun wasn't centered when not aiming, but wasn't sure what would be a good possible work around without jeopardizing the muzzleflash's and gunsound's origin.

    Thanks for the quick and clear answer! I will implement a second bulletspawn point so the bullets do get spawned from the center, preserving the origin of both the muzzleflash and the gunsounds :)