Quote: "What I need is a model to rotate towards the player when the player is 1200 away from it, move forward exactly 1000 in exactly 5 seconds, then run an animation. Then I need it to do the exact opposite back to its original position when E is pressed."
do you mean the
exact opposite like this?
local state = "wait"
local last_frame = 0
local first_frame = 0
function move_test_init(e)
CollisionOff(e)
end
function move_test_main(e)
if state == "wait" then
if GetPlayerDistance(e) < 1200 then
SetFreezePosition(g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ)
SetFreezeAngle(0,PlayerAngleToPoint(g_Entity[e]['x'],g_Entity[e]['z']),0)
TransportToFreezePosition()
FreezePlayer()
RotateToPlayerSlowly(e,2)
if EntityLookingAtPlayer(e,1300,2) == 1 then
StartTimer(e)
secs_to_take = 5
start_time = g_Time
start_dist = GetPlayerDistance(e)
startx = g_Entity[e]['x']
startz = g_Entity[e]['z']
state = "move towards player"
end
end
elseif state == "move towards player" then
RotateToPlayerSlowly(e,2)
if GetPlayerDistance(e) > start_dist-1000 then
time_perc = GetTimer(e)/(secs_to_take*1000)
dist_per_frame = start_dist*time_perc
new_y = math.rad(g_Entity[e]['angley'])
dest_x = startx + (math.sin(new_y) * dist_per_frame)
dest_z = startz + (math.cos(new_y) * dist_per_frame)
SetPosition(e,dest_x,g_Entity[e]['y'],dest_z)
else
SetAnimation(5)
PlayAnimation(e)
g_Entity[e]['animating'] = 1
last_frame = 1
state = "animate"
end
elseif state == "animate" then
if g_Entity[e]['animating'] == 0 then
Prompt("Press E to escape")
if g_KeyPressE == 1 then
frame = last_frame
state = "animate2"
end
else
if first_frame == 0 then
first_frame = GetAnimationFrame(e)
end
last_frame = GetAnimationFrame(e)
end
elseif state == "animate2" then
SetAnimationFrame(e,frame)
if GetTimer(e) > 10 then
StartTimer(e)
frame = frame - 1
end
if frame < first_frame then
new_startx = g_Entity[e]['x']
new_startz = g_Entity[e]['z']
state = "move away from player"
end
elseif state == "move away from player" then
if GetDistanceToPoint(e,startx,startz) > 80 then
time_perc = GetTimer(e)/(secs_to_take*1000)
dist_per_frame = start_dist*time_perc
new_y = math.rad(g_Entity[e]['angley'])
dest_x = new_startx + (math.sin(new_y) * -dist_per_frame)
dest_z = new_startz + (math.cos(new_y) * -dist_per_frame)
SetPosition(e,dest_x,g_Entity[e]['y'],dest_z)
else
UnFreezePlayer(e)
state = ""
--Destroy(e)
end
end
end
function PlayerAngleToPoint(x,z)
local destx = x - g_PlayerPosX
local destz = z - g_PlayerPosZ
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
if angle < 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
--SetRotation(e,0,angle,0)
return angle
end
function GetDistanceToPoint(e,x,z)
if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then
local disx = g_Entity[e]['x'] - x
local disz = g_Entity[e]['z'] - z
return math.sqrt(disx^2 + disz^2)
end
end
function EntityLookingAtPlayer(e,dis,v)
if g_Entity[e] ~= nil then
if dis == nil then
dis = 3000
end
if v == nil then
v = 0.5
end
local destx = g_PlayerPosX - g_Entity[e]['x']
local destz = g_PlayerPosZ - g_Entity[e]['z']
if math.sqrt((destx*destx)+(destz*destz)) <= dis then
local angle = math.atan2(destx,destz)
angle = angle * (180.0 / math.pi)
if angle <= 0 then
angle = 360 + angle
elseif angle > 360 then
angle = angle - 360
end
while g_Entity[e]['angley'] < 0 or g_Entity[e]['angley'] > 360 do
if g_Entity[e]['angley'] <= 0 then
g_Entity[e]['angley'] = 360 + g_Entity[e]['angley']
elseif g_Entity[e]['angley'] > 360 then
g_Entity[e]['angley'] = g_Entity[e]['angley'] - 360
end
end
local L = angle - v
local R = angle + v
if L <= 0 then
L = 360 + L
elseif L > 360 then
L = L - 360
end
if R <= 0 then
R = 360 + R
elseif R > 360 then
R = R - 360
end
if (L < R and math.abs(g_Entity[e]['angley']) > L and math.abs(g_Entity[e]['angley']) < R) then
return 1
elseif (L > R and (math.abs(g_Entity[e]['angley']) > L or math.abs(g_Entity[e]['angley']) < R)) then
return 1
else
return 0
end
else
return 0
end
end
end