Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Raycast twitches camera on load

Discussion in 'Scripting' started by angeloaf20, May 21, 2024.

  1. angeloaf20

    angeloaf20

    Joined:
    May 9, 2023
    Posts:
    3
    Hello,

    I have been working on a small interaction system for my game using a raycast that shoots from the player's (first person) camera. Right now I'm just testing with cubes to which I attached a tag, and when the raycast interaction occurs, a message appears on the screen.

    The weird thing is that, when the game starts and I move my camera to face the cube, the camera twitches to the left, but then works normally after that. I'm not sure if this would be a big issue but it's weird and I'm not sure why it happens.

    Code (CSharp):
    1. using System;
    2. using TMPro;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. class InteractionController : MonoBehaviour
    7. {
    8.     [SerializeField] private TextMeshProUGUI _interactionMessage;
    9.     [SerializeField] private Canvas _gameGui;
    10.     private Ray _playerRay;
    11.     private RaycastHit _raycastHit;
    12.  
    13.     private void Start()
    14.     {
    15.         _interactionMessage.enabled = false;
    16.         Debug.Log(_gameGui.GetComponents<TextMeshProUGUI>().Length);
    17.     }
    18.  
    19.  
    20.     private void Update()
    21.     {
    22.         CheckForColliders();
    23.         //CheckForInput();
    24.     }
    25.  
    26.     private void FixedUpdate()
    27.     {
    28.         CheckForColliders();
    29.     }
    30.  
    31.     void CheckForInput()
    32.     {
    33.         if (_interactionMessage.enabled == false) return;
    34.         if (Input.GetKeyDown(KeyCode.E))
    35.         {
    36.             if (_gameGui.GetComponents<TextMeshProUGUI>().Length == 0)
    37.             {
    38.                 TextMeshProUGUI newText = _gameGui.AddComponent<TextMeshProUGUI>();
    39.                 newText.text = "Test message";
    40.                 Debug.Log(_gameGui.GetComponents<TextMeshProUGUI>().Length);
    41.             }
    42.         }
    43.     }
    44.  
    45.     void CheckForColliders()
    46.     {
    47.         _playerRay = new Ray(transform.position, transform.forward);
    48.         if (Physics.Raycast(_playerRay, out _raycastHit) && _raycastHit.collider.gameObject.tag == "Interactable")
    49.         {
    50.             _interactionMessage.enabled = true;
    51.             _interactionMessage.text = $"Interactable: [E]";
    52.         }
    53.  
    54.         else
    55.         {
    56.             _interactionMessage.enabled = false;
    57.         }
    58.     }
    59. }
    I narrowed down the problem to being the CheckForColliders() function. Any help is appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    I don't think anything camera-twitchy-like is happening there.

    Based on this statement:

    My random wild guess is that more likely your mouse look code isn't reading the mouse once before using the difference each frame, making the first difference unusually large.

    Anyway, time to start debugging, even as simply as disabling chunks of code temporarily, but obviously it might require more investigation than that.

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
  3. angeloaf20

    angeloaf20

    Joined:
    May 9, 2023
    Posts:
    3
    Looking further into it, I think that the source of the issue is enabling/disabling the interaction message TextMeshPro object. If I set it to an empty string or to the interaction message, then I don't get that same "twitch", but this feels like a hacky solution and I'm not a fan of it.
     
  4. angeloaf20

    angeloaf20

    Joined:
    May 9, 2023
    Posts:
    3
    Update:

    I think this is an editor bug. I was thinking that I shouldn't have tried enabling and disabling thing in the editor. I got curious and built the game, tested it out, and I didn't get the camera twitch. A bit strange but if it doesn't reoccur then I'm not worried.