Search Unity

Raycasting Problem

Discussion in 'Scripting' started by Heu, Dec 24, 2014.

  1. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Hello, thank you for taking your time on reading this! :)

    So here is my issue,

    I am creating a first person shooter-esque prototype and I am using raycasting to shoot. The problem is, whenever I look down or up, the raycast doesn't go foward with the camera rotation.


    Here is a (rather large) image showing my problem.
    Untitled.png

    Here is my script

    Code (CSharp):
    1. RaycastHit hit;
    2. if(Physics.Raycast(transform.position, transform.forward, out hit, 50f))
    3. {
    4. Debug.DrawLine (transform.position, hit.point, Color.cyan);
    5. }

    Anything is appreciated.
     
    Last edited: Dec 24, 2014
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Cam transform is what you should be referencing. Are you?
     
  3. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Well this script is on the camera, so it should automatically use the camera's transform I assume.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Then I am not to sure. At worst, you could place two empty transforms in the cam tranform. Center them. Then take one and move it back on the z axis. Now you have two points that will always give you the direction vector.
     
  5. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    I've done that, still gives me the same problem. Quite an odd problem.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Odd.
    The transform.forward, should be whatever forward direction transform is in.


    The only thing that makes sense is that transform, is not camera.
     
  7. Hypocrita_20XX

    Hypocrita_20XX

    Joined:
    Feb 8, 2014
    Posts:
    27
    Hello and good evening!


    My first suggestion is to do a Debug.Log() of transform.gameObject.name, I believe it is, and see what exactly the transform is.
    That will return the name of the game object that the transform is associated with.

    If that is indeed the camera, then I would truthfully have no idea what the problem could be as the transform of the camera is, well, the camera.
    Now, if the transform is the camera, but still giving incorrect values, what I would personally do is something like the following.

    Code (CSharp):
    1. RaycastHit hit;
    2.  
    3. if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 50f))
    4. {
    5. Debug.DrawLine(Camera.main.transform.position, hit.point, Color.cyan);
    6. }
    This would ensure that you are indeed getting the main camera's transform, and not something random.
    And if all that still fails, then check to see if your camera is tagged as "MainCamera" to ensure that you do indeed have a main camera in the scene.


    Other than that, I can't say I have any other ideas, but I do hope this helps!
    And if you have any questions, do let me know! I'll answer as soon as I have the time.

    -Hypocrita
     
  8. Atmey

    Atmey

    Joined:
    Nov 3, 2012
    Posts:
    88
    I used to have something similar, but this little trick solved the issue for me:
    Code (CSharp):
    1. Debug.DrawRay (camera.position, targetTransform.position - camera.position);
    EDIT: ops, I misread, I thought you want it to point to a certain transform.
     
  9. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Okay thank you all for your help!

    I have found my issue, and it's quite odd. Here's what seems to be the problem...

    So I am using the Unity Assets(Beta) from the asset stores and I am using the First Person Controller. This controls the camera indirectly, as the camera is a child of the first person controller.

    I have the first person controller set disabled in the inspector, and I have a script that turns it back on. THIS APPEARS TO BE THE PROBLEM. When my scripts turns on the first person controller script, it acts funky and weird, but I paused the game while in Play Mode, and unchecked and checked the first person controller to be enabled again and what do you know. It works just fine!

    I then left my first person controller enabled and removed the script that remotely turns it on, and the raycast works fine... Quite an odd problem! I'm not sure why disabling/enabling a script component would make it not work.

    My guess is that, because the newest first person controller does something strange (embeds the two scripts inside??) with the MouseLook and HeadBob scripts and so when I turn that off and turn it back on I only turn on the first person controller and not the MouseLook && HeadBob scripts? Not sure, but a strange problem indeed.

    If anyone knows anything about this, I would like to know more! Thanks!