Need help with GameObject placement

What I’m trying to do here is to place a GameObject seed on the ground infront of the player on the ground, but I get weird results; When I jump, it gets placed in the air and so on…

How can I get it to always be placed on the terrain infront of the player / where the player is looking?

What I tried was to subtract 1 from playerPosition.y and add 1 to playerPosition.z but that didn’t help much at all.

Here’s my code:

using UnityEngine;
using System.Collections;

public class PlaceSeeds : MonoBehaviour {
	
	// Use Player GameObject
	public GameObject player;

	// Use a seed model
	public GameObject seedToPlaceModel;

	private Vector3 objectSpawnPosition;
	private Vector3 playerPosition;
	
	// Update is called once per frame
	void Update () {
		playerPosition = player.transform.position;
		objectSpawnPosition = new Vector3 (playerPosition.x, playerPosition.y, playerPosition.z);

		// If we leftclick (Mousebutton)
		if (Input.GetKeyDown (KeyCode.Mouse0)) {
			Instantiate (seedToPlaceModel, objectSpawnPosition, Quaternion.identity);
		}
	}
}

First, are you making a FPS game, or something like that?
If that is the case, what you can do is to create an empty object, attach it to the Camera of the FPS Player Prefab and put the object in front of the camera.

What you are looking for is the Physics.Raycast() function ( [see doc] (Unity - Scripting API: Physics.Raycast) ) :

Physics.Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

To be used like this:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
float maxDistance = 100; // in Unity units

if (Physics.Raycast(ray, out hit, maxDistance))
{
    Debug.Log("The mouse hit a collider at " + hit.point);
    // Draw a line to see the ray
    Debug.DrawLine(ray.origin, hit.point);
}

Note you can use the layerMask parameter to only hit terrains instances by placing each of your terrains in a specific layer