Legacy GM View via lengthdir

Strakee

Member
Hi, so I've been trying to make an object_view be in the half of the distance between my object player and the nearest enemy so I can have more dynamic views. If solved I will do a max distance but that I can figure out myself.

Here's my code and it just simply doesn't work as intended on paper. I want it to get the distance between those two objects and the direction and set it's location on the exact half between them.

p=obj_player
k=obj_enemy_parent


var ex, ey;
ex = instance_nearest(x, y, k).x;
ey = instance_nearest(x, y, k).y;


len= point_distance(p.x,p.y,ex,ey)
dir= point_direction(p.x,p.y,ex,ey)

xx= lengthdir_x(len,dir )
yy= lengthdir_y(len,dir)



move_towards_point(xx/2,yy/2,2)


This is in the end step event of the view object by the way.
 

obscene

Member
xx= lengthdir_x(len,dir )
yy= lengthdir_y(len,dir)

This coordinate is right, but it's relative to 0 so you need to add it to the player x and y.

ie...

xx= p.x+lengthdir_x(len,dir )
yy= p.y+lengthdir_y(len,dir)
 
P

PandaPenguin

Guest
and you are "halfing" at the wrong place
Code:
xx= lengthdir_x(len / 2,dir )
yy= lengthdir_y(len / 2,dir)
 

Strakee

Member
Today I came up with a much better and smoother idea than my previous one. I did this piece of code, but I have to play with the numbers to get the optimal result.

var p=obj_player
var k=obj_enemy_abstract

var a=max(p.x,k.x)
var b=min(p.x,k.x)
var c=max(p.y,k.y)
var d=min(p.y,k.y)

var xx = (a+b)/2
var yy = (c+d)/2

min_dis=point_distance(x,y,xx,yy)
max_dis=100
dis=point_distance(p.x,p.y,k.x,k.y)

if(dis<max_dis && min_dis>2)


{move_towards_point(xx,yy,0.5) }

else if(point_distance(x,y,p.x,p.y)>2)

{ move_towards_point(p.x,p.y,3) }


Thanks for replying anyway :)

I know you don't have to write every single var, you can write it after just one var, but I keep it for a cleaner overview.
 
Last edited:
Top