Copter doesn't hold altitude when navigating to target location in SITL

Hi there!

I am currently using the SITL simulator in Mission Planner to test a lua script. I am trying to enable the script to autonomously navigate the copter to a known GPS coordinate when the battery level reaches a certain percentage.

Currently, I have the copter navigating to the set coordinates. However, the copter keeps loosing altitude as it progresses closer and closer to the target location until it reaches 0m in altitude.

My question is, how can I have the copter navigate to the target location, without loosing any altitude and maintaining the target altitude set in the lua script.

This is the line of code I was attempting to use to set the altitude of the copter:
target:relative_alt(target:relative_alt() + (TARGET_ALT_ABOVE_TARGET * 100))

This is my first attempt at the Lua scripting so pls be patient :). Any help would be greatly appreciated.

Many thanks!

Sam

test.lua (1.4 KB)