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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Raycasting rotated in build

Discussion in 'Scripting' started by thel33tdood35, Apr 30, 2023.

  1. thel33tdood35

    thel33tdood35

    Joined:
    Jan 30, 2021
    Posts:
    9
    I'm running my game at the same 1440x900 resolution in both final build and editor, but when I build my game, any raycasts from the main camera are rotated slightly. and what's worse, the rotation gets worse the further away the player is from 0,0,0 (world space).
    upload_2023-4-30_12-28-50.png
    bullets in the editor and close to where the player spawns are fine,
    upload_2023-4-30_12-34-26.png
    but bullets shot from further away are offset by a ton.
    upload_2023-4-30_12-38-25.png
    red is where the player spawns, and black is where I've seen it most often/exaggerated

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.         {
    3.             if(shoottimer == 0)
    4.             {
    5.                 Vector3 dir = cam.transform.forward;
    6.                 if (lastshot < 20 && !Input.GetMouseButton(1))
    7.                 {
    8.                     dir.x += UnityEngine.Random.Range((21 - lastshot) * -0.005f, (21 - lastshot) * 0.005f);
    9.                     dir.y += UnityEngine.Random.Range((21 - lastshot) * -0.005f, (21 - lastshot) * 0.005f);
    10.                     dir.z += UnityEngine.Random.Range((21 - lastshot) * -0.005f, (21 - lastshot) * 0.005f);
    11.                 }
    12.                 RaycastHit hit;
    13.                 shoottimer = 15;
    14.                 lastshot = 0;
    15.                 Debug.Log("fired shot");
    16.                 shootaudio.Play();
    17.                 if (Physics.Raycast(cam.transform.position, dir, out hit, Mathf.Infinity))
    18.                 {
    19.                     Debug.DrawRay(transform.position, dir * hit.distance, Color.yellow, 5000);
    20.                     if (hit.collider.tag == "enemy")
    21.                     {
    22.                         hit.collider.gameObject.GetComponent<Health>().TakeDamage(25);
    23.                         if(hit.collider.gameObject.GetComponent<Health>().health < 25)
    24.                         {
    25.                             //killed! goober
    26.                             killaudio.Play();
    27.                         }
    28.                         Debug.Log("player hit bozo");
    29.                     }
    30.                     else
    31.                     {
    32.                         GameObject bh = Instantiate(bulletholeprefab, hit.point, Quaternion.FromToRotation(transform.up, hit.normal));
    33.                         bh.transform.up = hit.normal;
    34.                     }
    35.                 }
    that's the code that handles the raycasts and the instancing, and I don't see anything there that'd change in build. time specific variables like shoottimer and lastshot are both updated in fixedupdate.
     

    Attached Files:

  2. thel33tdood35

    thel33tdood35

    Joined:
    Jan 30, 2021
    Posts:
    9
    after removing the shooting innacurracy to see if that was the reason, I've found that the rotation flipflops between no rotation, (shooting forwards, what I want) and rotated towards the left,and towards the right. I still have no clue what's happening though.
    upload_2023-4-30_12-56-44.png upload_2023-4-30_12-56-53.png
     
  3. thel33tdood35

    thel33tdood35

    Joined:
    Jan 30, 2021
    Posts:
    9
    I have no clue why, but moving the above code to lateUpdate() seemed to fix the issue.