Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast does not always follow camera position

Discussion in 'Scripting' started by steven31dd, Jul 3, 2019.

  1. steven31dd

    steven31dd

    Joined:
    May 19, 2015
    Posts:
    5
    New Bitmap Image (3).jpg

    So I have this issue, where the raycast 90% of the time does not fire at the cameras current location.

    The setup is like the standard asset FPSController but modified.
    -I have tried getting viewport ray, screen ray, localPosition, position. Viewport ray manages to work sometimes (shown in image).
    -revisited code several times, no script bounces the camera position.
    -a muzzle raycast for shooting works perfectly well.

    would appreciate the help, any ideas?
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Since you are not showing any code, this is hard to diagnose, so it would help if you Show us how you set up the raycast. A blind guess is that you are using local position and local rotation to set up the cast on an object that is parented to the Player.
     
  3. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Also, you might want to explain your picture. I can sort of guess what it means, but it doesn't hurt to be clear.
     
  4. steven31dd

    steven31dd

    Joined:
    May 19, 2015
    Posts:
    5
    As mentioned multiple methods of using raycast from the camera were used.

    To clarify the picture a little better:
    the check mark was a correct ray shot,
    the X was an incorrect ray shot that fired underneath the player model,
    the ? was signifying that the camera's localPostion had not moved from the raycasts at all.


    Code (CSharp):
    1. // Interaction.cs
    2. ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0));
    3.  
    4.         if(Input.GetKeyDown(KeyCode.E)){
    5.             Debug.DrawRay(ray.origin, ray.direction, Color.blue, 5f);
    6.            
    7.             if(Physics.Raycast(ray, 2f, InteractMask)){
    8.                
    9.                 hit.collider.gameObject.SendMessage("Interact", SendMessageOptions.DontRequireReceiver);
    10.             }
    11.         }
    Code (CSharp):
    1. // Player FPS Snippet
    2.     void Start()
    3.     {
    4.  
    5.         if (mainCam == null)
    6.             mainCam = Camera.main.transform;
    7.  
    8.  
    9.             CC = GetComponent<CharacterController>();
    10.        
    11.         mouseLook = new CameraMouseLook();
    12.         mouseLook.Init(transform, mainCam);
    13.     }
    14.  
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if(Input.GetKey(KeyCode.LeftShift))
    20.             moveSpeed = runSpeed;
    21.         else
    22.             moveSpeed = walkSpeed;
    23.  
    24.      
    25.           //  oldPosition = transform.position;
    26.         if(CC.isGrounded) fallTimer = 0.0f;
    27.         else fallTimer += Time.deltaTime;
    28.            
    29.             HandleMovement();
    30.             mouseLook.Look(transform, mainCam);
    31.            // movement = transform.position - oldPosition;
    32.      
    33.         prevGounded = CC.isGrounded;
    34.     }
     
  5. steven31dd

    steven31dd

    Joined:
    May 19, 2015
    Posts:
    5

    EDIT: mouseLook is the exact same code of MouseLook in the standard assets minus the curosr lock.
     
  6. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    "the ? was signifying that the camera's localPostion had not moved from the raycasts at all." - Will never move because is the child of the player.

    Take the camera outside the player and search a camera script or just do it by yourself.

    Or just use the player position + rotation in raycast
     
  7. steven31dd

    steven31dd

    Joined:
    May 19, 2015
    Posts:
    5
    " Will never move because is the child of the player." - yes I am aware of this.

    I'll give your suggestion a try, I also might just use the muzzle transform as a interaction raycast position since it does work fine for shooting.
     
  8. steven31dd

    steven31dd

    Joined:
    May 19, 2015
    Posts:
    5
    I have the answer to my problem, if any ever has some issues similar to this...

    I noticed every time I fired the raycast, the position would drop lower and lower, what fixed this issue was setting the rigibody to kinematic.

    Even if your character doesn't visually fall or the transform doesn't show it, the raycast is still affected).
     
  9. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    If you look at the Inspector's Transform info, remember that by default it shows local coordinates. So if the object you used for reference for your raycast was parented to another object (as you did: the camera is parented to player), and that player has a rigidbody with gravity, then the whole player would be pulled down. The child obect containing the raycast will also move down but its local coords as shown in inspector will not change. I highly doubt that a raycast was affected by the RB. What likely was affected are the points you usd to feed into the raycast, and those point's frame of reference (parent object). Turning on isKinematic will remove all physisc, so gravity is also turned off, and the whole frame of reference remains where you expect it, and the input points for your raycast remained where you want.

    Perhaps try the following: uncheck isKiematic, and also uncheck Gravity. it should have the same effect (unless something else is moving your parent object), confirming that Raycast itself isn't affected by the RigidBody.
     
    Last edited: Jul 5, 2019
  10. shekalo

    shekalo

    Joined:
    Dec 21, 2017
    Posts:
    10
    Hey guys, for what it's worth I had a Camera following my Player using NavMesh (i.e. Camera has NavMesh component with Destination as Player)

    When I warped the Player elsewhere, the Camera's NavMesh couldn't follow (not contiguous navmeshes), though the 'camera' followed, the navmesh was stuck elsewhere. So what I needed to do was also Warp() the NavMesh to the new destination also