Hi everybody!
I’m trying to setup a building/waypoint/etc placement script in that would use the mouse cursor position to place the objects on the underlying terrain. I’ve got it working mostly, except for one thing - when I am placing the object the object starts jumping around on the screen… what I mean is:
Here’s the code relevant to this:
void Update ()
{
//if a new building/marker is being being placed (user hasnt finished placing it yet(hasnt clicked the lmb))
if (buildingInProgress == true)
{
placeObject();
if (Input.GetMouseButton(0))
{
buildingCreated = true; //only 1 building needs to be built
buildingInProgress = false;
}
}
}
void placeObject()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
//if a placement building was not yet created, create a new one
if (buildingBeingPlaced == null)
{
GameObject newBuilding = Instantiate(GameObject.Find(buildingName)) as GameObject;
newBuilding.name = "buildingBeingPlaced";
buildingBeingPlaced = (newBuilding);
Debug.Log("New placementBuilding created!");
}
//if the building already exists, just move it
else
{
GameObject.Find("buildingBeingPlaced").transform.position = new Vector3(hit.point.x, 65, hit.point.z);
}
Debug.DrawLine(GameObject.Find("buildingBeingPlaced").transform.position, hit.point, Color.yellow);
}
I don’t really get how the RaycastHit works and especially these 2 lines ofcourse:
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hit);
If somebody could give me some tips or point me to a solution/explanation of that, that would be much appreciated ![]()