Placement of object jittering when using ScreenPointToRay.

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:

                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        //transform.LookAt(hit.point);

                        Debug.LogWarning("hit: " + hit.point + " hit ob: " + hit.collider.gameObject);
                        Vector3 dir = (hit.point - transform.position );

                        transform.rotation = Quaternion.LookRotation(dir,Vector3.up);


                    }

Here is what I am currently using to determine the head position.

using UnityEngine;
using MLAPI;


public class LookRotation : NetworkBehaviour
{
    public bool freezeRotation;
    public GameObject mainBody;
    float xRotation;
    float yRotation;
    public float horizontalSpeed;
    public float verticalSpeed;

    void OnGUI()
    {
        if (IsLocalPlayer)
        {
            if(freezeRotation == false)
            {
                if (transform.parent.GetComponent<ChangeCameraView>().isOverview == false)
                {
                    float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
                    float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;

                    yRotation += mouseX;
                    xRotation -= mouseY;
                    xRotation = Mathf.Clamp(xRotation, -90, 90);

                    mainBody.transform.eulerAngles = new Vector3(0, yRotation, 0);
                    transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
                }
                else
                {

                    if (Input.GetMouseButton(2))
                    {
                        float mouseX = Input.GetAxis("Mouse X") * horizontalSpeed;
                        float mouseY = Input.GetAxis("Mouse Y") * verticalSpeed;

                        yRotation += mouseX;
                        xRotation -= mouseY;
                        xRotation = Mathf.Clamp(xRotation, -90, 90);

                        mainBody.transform.eulerAngles = new Vector3(0, yRotation, 0);




                    }
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {

                        Debug.LogWarning("hit: " + hit.point + " hit ob: " + hit.collider.gameObject);
                        Vector3 dir = (hit.point - transform.position );

                        transform.rotation = Quaternion.LookRotation(dir,Vector3.up);


                    }
                }
            }
        }
    }
}

Here is the code to determine the position of the chest.

   private void OnGUI()
    {
        RaycastHit hit;

        if (Physics.Raycast(transform.GetChild(0).transform.position, transform.GetChild(0).transform.forward, out hit, Mathf.Infinity))
        {
            if (placingItem == false)
            {
                if (newTemplate)
                {

                    if (newTemplate.GetComponent<SnapProperties>())
                    {
                        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);

                        tempWorldPos = gridSnap + newTemplate.GetComponent<BuildingProperties>().templateOffset;
                    }
                    else
                    {
                        Debug.LogWarning("hit ob: " + hit.collider.gameObject);
                        tempWorldPos =  hit.point + newTemplate.GetComponent<BuildingProperties>().templateOffset;

                    }

                }
                else
                {

                }
                if (Input.GetKeyDown(KeyCode.Delete))
                {
                    Debug.LogWarning("detected : " + hit.collider.gameObject);
                    if (hit.collider.GetComponent<BuildingProperties>())
                    {
                        if (hit.collider.GetComponent<BuildingProperties>().readyToBuild)
                        {
                            Debug.LogWarning("deleting: " + hit.collider.gameObject);

                            DestroyItemServerRpc(hit.collider.GetComponent<NetworkObject>().NetworkObjectId);

                        }
                    }
                    if (hit.collider.transform.parent)
                    {
                        if (hit.collider.transform.parent.GetComponent<BuildingProperties>())
                        {
                            if (hit.collider.transform.parent.GetComponent<BuildingProperties>().readyToBuild)
                            {
                                Debug.LogWarning("deleting: " + hit.collider.transform.parent.gameObject);
                                DestroyItemServerRpc(hit.collider.transform.parent.GetComponent<NetworkObject>().NetworkObjectId);

                            }
                        }
                    }

                }
            }
        }
    }

I hope you can point me in the right direction because im stumped. Any suggestions would be appreciated.

1 Like

bumping because I still have this problem