- Home /
The question is answered, right answer was accepted
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);
}
Answer by AaronBacon · Jun 25, 2020 at 03:02 PM
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: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html)
Thats not the Problem. The Debug.log givse me the change of the Turrent in the logs. But the Gameobject do not move...
Hm, only other thing i could think of then is that its moving the turret, but its moving it at an imperceivable ratio relative to the scene view. Perhaps try multiply hitInfo.point by a ridiculous amount like 10000 to see if that at least makes the movement viewable?
Also, just to check, The Turrent script isn't on a child gameObject right? (As it would only move the Child, not the parent)
The script is right to the prefab turrent. But thank you for your help.