Forum

> > CS2D > Scripts > Move player depending on path
Forums overviewCS2D overview Scripts overviewLog in to reply

English Move player depending on path

10 replies
To the start Previous 1 Next To the start

old Move player depending on path

MikuAuahDark
User Off Offline

Quote
Just like the name of title, i need a script that move player depending on a path that i defined. it need to take time for 250 frame(5000ms) to reach the path
I need this for my mission map, Reverser

Path List:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
MovingCamera={
	{
		{1,1},
		{33,9},
		{34,36},
		{13,28},
		{14,6}
	},
	{
		{27,63},
		{16,49},
		{27,63},
		{37,49},
		{17,62},
		{34,52}
	},
	{
		{3,89},
		{22,89},
		{20,108},
		{55,108},
		{55,131},
		{20,142},
		{43,90}
	}
}
don't worry what path to pick, just pick one of three that i listed above.
I already able to do it with math.atan2 but it always going to wrong path.
Everytine it reach the path, it going to next path

Ok so i need script that move player depending on path and reach the path for 250 frame.

old Re: Move player depending on path

Deleted User

Quote
1
2
3
function getAngle(x1, y1, x2, y2)
	-math.deg(math.atan2(x1 - x2, y1 - y2))
end
This function will give you the right angle to get you from point A(X1|Y1) to point B(X2|Y2)
1
2
3
function extendPos(x, y, distance, direction)
	return x + math.sin(math.rad(direction)) * distance, y - math.cos(math.rad(direction)) * distance
end
This function will extend your position by <distance> pixels, in given direction.

Then just use the ms100/always hook to move him.
edited 1×, last 14.06.13 09:49:04 am

old Re: Move player depending on path

EngiN33R
Moderator Off Offline

Quote
user MikuAuahDark has written
i already use that method. It always give a inaccurate angle to next path. Even sometimes it going more far from next path


Then your calculations are wrong. It's best that you post what you use here for future reference by other scripters.

old Re: Move player depending on path

MikuAuahDark
User Off Offline

Quote
@user EngiN33R & user Infinite Rain: Here the code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
MovingCamera={
	{
		{1,1},
		{33,9},
		{34,36},
		{13,28},
		{14,6}
	},
	{
		{27,63},
		{16,49},
		{27,63},
		{37,49},
		{17,62},
		{34,52}
	},
	{
		{3,89},
		{22,89},
		{20,108},
		{55,108},
		{55,131},
		{20,142},
		{43,90}
	}
}
CamSet={0,0}
function distance(x1,y1,x2,y2)
	return math.floor(math.sqrt((x2-x1)^2+(y2-y1)^2))
end

addhook("always","Auah_Dark")
function Auah_Dark()
	if MoveCam==true then
		if CamSet[1]==0 and CamSet[2]==0 then
			CamSet[1]=1
			CamSet[2]=math.random(1,#MovingCamera)
			setpos(1,MovingCamera[CamSet[2]][1][1]*32+16,MovingCamera[CamSet[2]][1][2]*32+16)
		elseif CamSet[1]==#MovingCamera[CamSet[2]] then
			MoveCam=false
			local tmp=image("gfx/block.bmp",320,240,2)
			imagescale(tmp,21,21)
			imagealpha(tmp,0)
			tween_alpha(tmp,1000,1)
			timerEx(1000,tween_alpha,1,tmp,1000,0)
			timerEx(2000,freeimage,1,tmp)
			timerEx(2000,loadstring("MoveCam=true"),1)
			CamSet[1]=0
			CamSet[2]=0
		else
			local x,y=player(1,"x"),player(1,"y")
			local a=math.abs(math.atan2(MovingCamera[CamSet[2]][CamSet[1]+1][2]-MovingCamera[CamSet[2]][CamSet[1]][2],MovingCamera[CamSet[2]][CamSet[1]+1][1]-MovingCamera[CamSet[2]][CamSet[1]][1])-(math.pi/2))
			x = x + math.cos(a) * (distance(MovingCamera[CamSet[2]][CamSet[1]][1]*32+16,MovingCamera[CamSet[2]][CamSet[1]][2]*32+16,MovingCamera[CamSet[2]][CamSet[1]+1][1]*32+16,MovingCamera[CamSet[2]][CamSet[1]+1][2]*32+16)/250)
			y = y + math.sin(a) * (distance(MovingCamera[CamSet[2]][CamSet[1]][1]*32+16,MovingCamera[CamSet[2]][CamSet[1]][2]*32+16,MovingCamera[CamSet[2]][CamSet[1]+1][1]*32+16,MovingCamera[CamSet[2]][CamSet[1]+1][2]*32+16)/250)
			setpos(1,x,y)
			hudtxt(9,"\""..distance(x,y,MovingCamera[CamSet[2]][CamSet[1]+1][1]*32+16,MovingCamera[CamSet[2]][CamSet[1]+1][2]*32+16).." "..CamSet[1].." "..CamSet[2].." "..MovingCamera[CamSet[2]][CamSet[1]][1].." "..MovingCamera[CamSet[2]][CamSet[1]][2].."\"",0,0,0)
			if math.sqrt(((MovingCamera[CamSet[2]][CamSet[1]+1][1]*32+16)-x^2)+((MovingCamera[CamSet[2]][CamSet[1]+1][2]*32+16)-y)^2)<=32 then
				CamSet[1]=CamSet[1]+1
			end
		end
	end
end
EDIT: MoveCam variable would triggered somewhere else on code at my Reverser lua

old Re: Move player depending on path

MikuAuahDark
User Off Offline

Quote
no luck. it's still sometimes going more far than next path and sometimes everytime i start Reverser, it just flashing first and then teleporting to new path

old Re: Move player depending on path

Deleted User

Quote
1. Make an invisible image, get it's id
2. tween_move it to the next path, set time to 5000
3. in always hook set player's pos to this image
4. after 5000 ms remove the image

Easy and simple way of doing + much less laggy.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview