In this Post we will learn how to find distance between two objects in Gdevelop 5. Many times we are required to find distance between two objects Gdevelop provides inbuilt functionality for this purpose just let's get started.
Creating A Project
We will start by creating an empty project. So click on create a new project and name it distance between two objects.
Let's quickly add a scene and a sprite.
Let's add another sprite. I will also add text to display the distance between these two sprites.
Let's add these objects to the scene.Now let's head over to the event sheet and Add an event.The condition will be empty and action will to display the distance. So choose the distance choose->modify text and operator be set to equal and click on then expression button. We first have to convert expression to string choose conversion and then number to text. Click on the expression button and then search square root.
Click on expression button again and this time then search square again and this time choose a square distance between two objects and select the two objects as new object one and new object two click on apply.
Let's make Objects draggable so that we can change the distance between them. Choose the new object and get to the behaviors add a behavior draggable.
Repeat the same for the other object
Now if we play can see that change in the distance we drag the objects around.
The square distance between these two objects gives us well the square of the distance between these
two objects. Now, if you take the root of this expression we get the real distance between them so this is why we use the square root function.
If you don't want fractional part then just get back to the event sheet and apply the floor function.
Okay so now you can play the game.The distance should now be displayed without the fractional part.
Download The File Here
Thank You Very Much For your Time
Comments
Post a Comment