Move gameObject with mouse

Hello,

i want to move a gameobject that i selected via a button on the mouse position. But the gameobject shows up in the scene and do not move at all. Here is my Code:

 private Turrent turrent;

    public void showPrefab(Turrent turrent)
    {
        this.turrent = turrent;
        Instantiate(turrent);
}

    
    }

    private void Update()
    {
        if(turrent!= null)
            moveTurrent();
    }

    public void moveTurrent()
    {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray, out hitInfo))
            {
                turrent.transform.position = hitInfo.point;
                Debug.Log(turrent.transform.position);
            }   
    }

It seems you are trying to move a prefab, not the actual instance..... public void showPrefab(Turrent turrent) { this.turrent = Instantiate(turrent); }

–

1 Answer

1

Is there an object for the Raycast to hit? Physics.Raycast will only return true if it actually hits something, so if it never hits any valid object that if statement will never be run (May want to research the Raycast documentation to check if there’s some other reason the Raycast isn’t hitting: Unity - Scripting API: Physics.Raycast)

Thats not the Problem. The Debug.log givse me the change of the Turrent in the logs. But the Gameobject do not move...

–