# How to get terrain height at X and Y coordinates in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803>\
**Category:** World Creation\
**Tags:** Terrain, height, line, unreal-engine, Spline, CPP, question\
**Created:** [April 24, 2019, 8:25pm UTC](https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803 "2019-04-24T20:25:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim\_Clow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jim\_Clow](https://forums.unrealengine.com/u/Jim_Clow)\
**Post date:** [April 24, 2019, 8:25pm UTC](https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803/1 "2019-04-24T20:25:02Z")

</div>

Hi , does anyone know how to get the z value for terrain at a particular x and y world coordinate using c++?

I could look at the height map itself to calculate heights, but all that would give me is a height of a peaks and troughs, rather than at some point in between them, which is what I’m after.

Googling around it would seem that a line trace may be the answer I am looking for, but I can’t find any C++ examples of this, or even anything similar that I could adapt for my purpose.

Ultimately I want to use C++ to align spline meshes to terrain. An easy job, if I can just get a terrain Z values whilst I’m getting the x,y location values along a spline.

---

<div class="post-metadata">

**Author:** ![BrunoCaxito](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/brunocaxito/32/706161_2.png) [@BrunoCaxito](https://forums.unrealengine.com/u/BrunoCaxito)\
**Post date:** [April 25, 2019, 12:23am UTC](https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803/2 "2019-04-25T00:23:18Z")

</div>

This is a simple example of an AActor method, you can search about LineTrace methods. Hope it helps.

**Header File**

```
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintPure, meta = ( AdvancedDisplay = "bDrawDebugLines" ))
		float GetSurface(FVector2D Point, bool bDrawDebugLines = false);
};

```

**Cpp**

```
#include "MyActor.h"

#include "Runtime/Engine/Classes/Engine/EngineTypes.h"
#include "Runtime/Engine/Classes/Engine/World.h"
#include "Runtime/Engine/Public/DrawDebugHelpers.h"

float AMyActor::GetSurface(FVector2D Point, bool bDrawDebugLines)
{
	UWorld* World{ this->GetWorld() };

	if (World)
	{
		FVector StartLocation{ Point.X, Point.Y, 1000 };	// Raytrace starting point.
		FVector EndLocation{ Point.X, Point.Y, 0 }; // Raytrace end point.

		// Raytrace for overlapping actors.
		FHitResult HitResult;
		World->LineTraceSingleByObjectType(
			OUT HitResult,
			StartLocation,
			EndLocation,
			FCollisionObjectQueryParams(ECollisionChannel::ECC_WorldStatic),
			FCollisionQueryParams()
		);

		// Draw debug line.
		if (bDrawDebugLines)
		{
			FColor LineColor;

			if (HitResult.GetActor()) LineColor = FColor::Red;
			else LineColor = FColor::Green;

			DrawDebugLine(
				World,
				StartLocation,
				EndLocation,
				LineColor,
				true,
				5.f,
				0.f,
				10.f
			);
		}

		// Return Z location.
		if (HitResult.GetActor()) return HitResult.ImpactPoint.Z;
	}

	return 0;
}

```

**Blueprint**

 ![275117-capturar.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/7/9/4797bd106b261a4f724e7dcf17995f64684ea2fc.png)

---

<div class="post-metadata">

**Author:** ![Jim\_Clow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jim\_Clow](https://forums.unrealengine.com/u/Jim_Clow)\
**Post date:** [April 25, 2019, 7:43am UTC](https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803/3 "2019-04-25T07:43:09Z")

</div>

Thanks Newest, that’s extremely helpful, I can use the X and Y from getdiatancealongspline at increments and just set my start and end locations as well above and below my terrain. Happy days 🙂

---

<div class="post-metadata">

**Author:** ![Katianie](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Katianie](https://forums.unrealengine.com/u/Katianie)\
**Post date:** [January 4, 2025, 10:47pm UTC](https://forums.unrealengine.com/t/how-to-get-terrain-height-at-x-and-y-coordinates-in-c/447803/4 "2025-01-04T22:47:57Z")

</div>

I think I found a way to do it without the line trace. The ALandscape class has a function called GetHeightAtLocation that takes in world position and returns the height at that location. Here is the code:

```auto
AActor* landscapeActor = UGameplayStatics::GetActorOfClass(GetWorld(),ALandscape::StaticClass());
ALandscape* theLandscape = Cast<ALandscape>(landscapeActor);
float heightAtLocation = theLandscape->GetHeightAtLocation(GetActorLocation()).GetValue();

```
