RaycastHit question

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 :slight_smile:

…anyone? any ideas whatsoever?

I can’t tell just from the code you’ve posted, but do you have some way to make sure the raycast doesn’t hit the building itself? That seems like it could cause the effect you’re seeing.

I’m sure you’ve found it already but the online docs describe the physics.raycast well.

The first line is referencing your main camera in the scene and creates a worldspace transform based on the cameras transform and the mouse cursor location (screen space).

The second line projects that point out in to the world using the inherited camera orientation until it hits a collider. the result of the hit is stored in to the hit parameter you pass in.

It is possible to visualize the ray by adding the following line after this raycast.

Debug.DrawLine (ray.origin, hit.point);

you might also want to investigate using a layermask during the raycast. With this you would put your lanscape in to it’s own layer and then add this layer as a parameter to your raycast. doing this will only return hits where the ray hits an object on the masked layer (ie your landscape).