-- LUA Script - precede every function and global member with lowercase name of script + '_main' local rad = math.rad local deg = math.deg local atan = math.atan2 local sqrt = math.sqrt local Q = require "scriptbank\\quatlib" local facingEnts = {} local rotated = {} function facing_init(e) Include("quatlib.lua") end function FindFacing( ommitting ) -- return first entity in list that isn't the one specified for k, v in pairs( facingEnts ) do if v and k ~= ommitting then return k end end end function facing_main(e) local thisEnt = facingEnts[e] if thisEnt == nil then facingEnts[e] = true return elseif not rotated[ e ] then local otherEnt = FindFacing( e ) if otherEnt == nil then return end -- rotate to face it local oEnt = g_Entity[ otherEnt ] local tEnt = g_Entity[ e ] local myQ = Q.FromEuler( 0, 0, 0 ) -- our default direction -- now work out the angles from us to them local DX, DZ = oEnt.x - tEnt.x, oEnt.z - tEnt.z local yAng = atan( DX, DZ ) local DY = oEnt.y - tEnt.y local xAng = -atan( DY, sqrt( DX*DX + DZ*DZ ) ) myQ = Q.Mul( myQ, Q.FromEuler( 0, yAng, 0 ) ) myQ = Q.Mul( myQ, Q.FromEuler( xAng, 0, 0 ) ) local xr, yr, zr = Q.ToEuler( myQ ) CollisionOff( e ) SetRotation( e, deg(xr), deg(yr), deg(zr) ) ResetRotation( e, deg(xr), deg(yr), deg(zr) ) CollisionOff( e ) rotated[ e ] = true end end