Moving gameobject along terrain based on player movement using raycast

I want to be able to have objects that are always on the ground, but move based on the player position. Because of hills in the terrain the object needs to be able to raise and lower in the Y axis as the ground goes up and down. The solution I came up with is to have the parent object, that is a child of the player, raycast to the ground and then adjust the child object’s y position based on that. However I can not for the life of me figure out how to get it to work.

This is the code I tried. Any insight would be much appreciated.

Thanks :slight_smile:

selectedItem is the object I want to appear on the ground.

placeSpot is the parent object that is a child of the player.

In the place of hit.normal.y I have also tried tempY3, hit.point.y, hit.transform.localPosition.y, and hit.transform.position.y

void Update () 
	{

		Debug.DrawRay(placeSpot.transform.position, -Vector3.up, Color.green);


		if(placingItem)
		{
			RaycastHit hit;
			Physics.Raycast(placeSpot.transform.position, Vector3.down, out hit);
			if(Physics.Raycast(placeSpot.transform.position, Vector3.down, 100))
			{
				Debug.Log(hit.distance);
				tempY = hit.distance;
				float tempY2 = selectedItem.transform.localPosition.y;
				float tempY3 = tempY - tempY2;
				selectedItem.transform.localPosition = new Vector3(placeSpot.transform.localPosition.x, hit.normal.y, placeSpot.transform.localPosition.z);
			}
			selectedItem.transform.rotation = Quaternion.identity;
			selectedItem.transform.parent = placeSpot.transform;
			selectedItem.transform.localRotation = placeSpot.transform.localRotation;
		}
	}

Well, first of all, there is no need to raycast twice. Physics.Raycast returns true when it hits something. Currently you are using the first raycast it’s hit when the second raycast hits something, that doesn’t really make sense.

Also, read the docs of RaycastHit. hit.point is probably th Vector3 you are looking for.

I added a piece of code that should place the ‘selectedItem’ at the first thing it hits when casting a ray down. If it places it at the exact same position as placeSpot. Then placeSpot probably has a collider attatched to it that you hit. In that case you’ll need to ignore the placeSpot’s collider (Example is for linecast, but the layerMask parameter works in the same way)

        float distance = 100; // I use this value to make sure the Debug ray is identical (Of the same length) to the Ray I cast.
        Debug.DrawRay(placeSpot.transform.position, Vector3.down * distance, Color.green);

        if (placingItem)
        {
            RaycastHit hit;
            if (Physics.Raycast(placeSpot.transform.position, Vector3.down, distance, out hit))
            {
                Debug.Log(hit.point);
                selectedItem.transform.parent = placeSpot.transform;
                selectedItem.transform.localRotation = placeSpot.transform.localRotation;
                selectedItem.transform.position = hit.point;
            }
        }

If this does not help for your problem, then please describe your problem better. Because a.t.m. you are telling us what your code is, you are telling us what it should do, but you aren’t telling us what it actually does (is the Debug.Log logged, where is your selectedItem placed? etc.)