Find Closest Point to Player Above Ground

Hello,

I am working on a 3D game and I have come across a problem I can’t solve. I am trying to find the closest point on an object from my player, but I need to know the closest point above the terrain or above a y-value. Is this possible?

Thank you

You can use an Edge Collider 2D, and then you might be able to just edit the collider from a script to only be above the terrain. Unity has some pretty good documentation, here’s the link to Edge Collider 2D https://docs.unity3d.com/ScriptReference/EdgeCollider2D.html

Something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetEdgeCollider : MonoBehaviour
{

	public Collider2D collider;
	public Vector2[] colliderPoints;
	public float groundLevel;

	void Start()
	{
		colliderPoints = collider.points;
		colliderPoints[2] = new Vector2(colliderPoints[2].x, groundLevel);
		colliderPoints[3] = new Vector2(colliderPoints[3].x, groundLevel);
	    colliderPoints[4] = new Vector2(colliderPoints[4].x, groundLevel);
        colliderPoints[5] = new Vector2(colliderPoints[5].x, groundLevel);
		collider.points = colliderPoints;
	}
	
}