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

Placement of object jittering when using ScreenPointToRay.

Discussion in 'Physics' started by EggnoggOfEgg, Feb 9, 2022.

  1. EggnoggOfEgg

    EggnoggOfEgg

    Joined:
    Jan 23, 2021
    Posts:
    5
    Hello, so im using a raycast from the main camera to the mouse position to determine the position of placing objects down (in this case a chest), but I get really bad jittering. I have deduced that it is most likely something to do with the raycast itself. I have Tried lerping, using fixedUpdate, LateUpdate and OnGui and it doesn't make any difference. The camera is not moving at all so I know its not the camera as well. I do get slight changes in position with the ray cast even though the mouse is still, not sure if it is related to this.

    I have also isolated the code to simply raycast and set position based on this code:

    Code (CSharp):
    1.      
    2.          
    3.                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.                     RaycastHit hit;
    5.                     if (Physics.Raycast(ray, out hit))
    6.                     {
    7.                         //transform.LookAt(hit.point);
    8.  
    9.                         Debug.LogWarning("hit: " + hit.point + " hit ob: " + hit.collider.gameObject);
    10.                         Vector3 dir = (hit.point - transform.position );
    11.  
    12.                         transform.rotation = Quaternion.LookRotation(dir,Vector3.up);
    13.  
    14.  
    15.                     }
    Here is what I am currently using to determine the head position.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using MLAPI;
    4.  
    5.  
    6. public class LookRotation : NetworkBehaviour
    7. {
    8.     public bool freezeRotation;
    9.     public GameObject mainBody;
    10.     float xRotation;
    11.     float yRotation;
    12.     public float horizontalSpeed;
    13.     public float verticalSpeed;
    14.  
    15.     void OnGUI()
    16.     {
    17.         if (IsLocalPlayer)
    18.         {
    19.             if(freezeRotation == false)
    20.             {
    21.                 if (transform.parent.GetComponent<ChangeCameraView>().isOverview == false)
    22.                 {
    23.                     float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
    24.                     float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;
    25.  
    26.                     yRotation += mouseX;
    27.                     xRotation -= mouseY;
    28.                     xRotation = Mathf.Clamp(xRotation, -90, 90);
    29.  
    30.                     mainBody.transform.eulerAngles = new Vector3(0, yRotation, 0);
    31.                     transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
    32.                 }
    33.                 else
    34.                 {
    35.  
    36.                     if (Input.GetMouseButton(2))
    37.                     {
    38.                         float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
    39.                         float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;
    40.  
    41.                         yRotation += mouseX;
    42.                         xRotation -= mouseY;
    43.                         xRotation = Mathf.Clamp(xRotation, -90, 90);
    44.  
    45.                         mainBody.transform.eulerAngles = new Vector3(0, yRotation, 0);
    46.  
    47.  
    48.  
    49.  
    50.                     }
    51.                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    52.                     RaycastHit hit;
    53.                     if (Physics.Raycast(ray, out hit))
    54.                     {
    55.  
    56.                         Debug.LogWarning("hit: " + hit.point + " hit ob: " + hit.collider.gameObject);
    57.                         Vector3 dir = (hit.point - transform.position );
    58.  
    59.                         transform.rotation = Quaternion.LookRotation(dir,Vector3.up);
    60.  
    61.  
    62.                     }
    63.                 }
    64.             }
    65.         }
    66.     }
    67. }
    68.  
    Here is the code to determine the position of the chest.

    Code (CSharp):
    1.    private void OnGUI()
    2.     {
    3.         RaycastHit hit;
    4.  
    5.         if (Physics.Raycast(transform.GetChild(0).transform.position, transform.GetChild(0).transform.forward, out hit, Mathf.Infinity))
    6.         {
    7.             if (placingItem == false)
    8.             {
    9.                 if (newTemplate)
    10.                 {
    11.  
    12.                     if (newTemplate.GetComponent<SnapProperties>())
    13.                     {
    14.                         Vector3 gridSnap = new Vector3(Mathf.RoundToInt(hit.point.x / 2) * 2, Mathf.RoundToInt(hit.point.y / 2) * 2, Mathf.RoundToInt(hit.point.z / 2) * 2);
    15.  
    16.                         tempWorldPos = gridSnap + newTemplate.GetComponent<BuildingProperties>().templateOffset;
    17.                     }
    18.                     else
    19.                     {
    20.                         Debug.LogWarning("hit ob: " + hit.collider.gameObject);
    21.                         tempWorldPos =  hit.point + newTemplate.GetComponent<BuildingProperties>().templateOffset;
    22.  
    23.                     }
    24.  
    25.                 }
    26.                 else
    27.                 {
    28.  
    29.                 }
    30.                 if (Input.GetKeyDown(KeyCode.Delete))
    31.                 {
    32.                     Debug.LogWarning("detected : " + hit.collider.gameObject);
    33.                     if (hit.collider.GetComponent<BuildingProperties>())
    34.                     {
    35.                         if (hit.collider.GetComponent<BuildingProperties>().readyToBuild)
    36.                         {
    37.                             Debug.LogWarning("deleting: " + hit.collider.gameObject);
    38.  
    39.                             DestroyItemServerRpc(hit.collider.GetComponent<NetworkObject>().NetworkObjectId);
    40.  
    41.                         }
    42.                     }
    43.                     if (hit.collider.transform.parent)
    44.                     {
    45.                         if (hit.collider.transform.parent.GetComponent<BuildingProperties>())
    46.                         {
    47.                             if (hit.collider.transform.parent.GetComponent<BuildingProperties>().readyToBuild)
    48.                             {
    49.                                 Debug.LogWarning("deleting: " + hit.collider.transform.parent.gameObject);
    50.                                 DestroyItemServerRpc(hit.collider.transform.parent.GetComponent<NetworkObject>().NetworkObjectId);
    51.  
    52.                             }
    53.                         }
    54.                     }
    55.  
    56.                 }
    57.             }
    58.         }
    59.     }
    I hope you can point me in the right direction because im stumped. Any suggestions would be appreciated.
     
    ksf000 likes this.
  2. EggnoggOfEgg

    EggnoggOfEgg

    Joined:
    Jan 23, 2021
    Posts:
    5
    bumping because I still have this problem