-
-
Save R3DIANCE/9e7bac4266354d97b843b757ad6f3ce9 to your computer and use it in GitHub Desktop.
alt:V player look sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import alt from "alt-client" | |
import native from "natives" | |
const localPlayer = alt.Player.local | |
const SYNC_MS_DELAY = 500 | |
const SYNC_MS_DELAY_REMOTE = 100 | |
const MAX_HEADING_DIST = 70.0 | |
const BACKWARDS_HEADING_DIST = 145.0 | |
const TASK_LOOK_AT_COORD_DURATION = 300 | |
const LOOKING_AT_POS_OFFSET = 5.0 | |
let lastSendedLookingAtPosString | |
const sendServer = (lookingAtPos) => { | |
let posString = "" | |
if (lookingAtPos) { | |
posString = lookingAtPos | |
.toArray() | |
.map(v => v.toFixed(1)) | |
.join("|") | |
} | |
if (lastSendedLookingAtPosString === posString) return | |
alt.emitServer( | |
"sync-player-look-at", | |
posString, | |
) | |
} | |
let prevPlayerHeading = -1 | |
let prevCamRot = alt.Vector3.zero | |
alt.setInterval(() => { | |
let camRot = native.getGameplayCamRot(2) | |
const playerHeading = localPlayer.rot.toDegrees().z | |
if ( | |
( | |
prevCamRot.x.toFixed(1) === camRot.x.toFixed(1) && | |
prevCamRot.y.toFixed(1) === camRot.y.toFixed(1) && | |
prevCamRot.z.toFixed(1) === camRot.z.toFixed(1) | |
) && | |
(playerHeading.toFixed(1) === prevPlayerHeading.toFixed(1)) | |
) return | |
prevCamRot = camRot | |
prevPlayerHeading = playerHeading | |
const camHeading = camRot.z | |
// if player is looking backwards block player head heading at MAX_HEADING_DIST | |
const headingDist = distanceDegrees(camHeading, playerHeading) | |
if (headingDist > BACKWARDS_HEADING_DIST) { | |
sendServer(null) | |
return | |
} | |
if (headingDist > MAX_HEADING_DIST) { | |
// why i wrote something like this? don't ask me | |
const a = playerHeading + MAX_HEADING_DIST | |
const b = playerHeading - MAX_HEADING_DIST | |
const aDist = distanceDegrees(camHeading, a) | |
const bDist = distanceDegrees(camHeading, b) | |
const closest = aDist < bDist ? a : b | |
camRot = new alt.Vector3(camRot.x, camRot.y, closest) | |
} | |
const direction = rotationToDirectionDegrees(camRot) | |
const lookingAtPos = direction.mul(LOOKING_AT_POS_OFFSET).add(localPlayer.pos) | |
sendServer(lookingAtPos) | |
}, SYNC_MS_DELAY) | |
alt.setInterval(() => { | |
for (const player of alt.Player.streamedIn) { | |
const posString = player.getStreamSyncedMeta("sync-player-look-at") | |
if (!posString) continue | |
const pos = posString | |
.split("|") | |
.map(v => +v) | |
native.taskLookAtCoord(player, ...pos, TASK_LOOK_AT_COORD_DURATION, 0, 0) | |
} | |
}, SYNC_MS_DELAY_REMOTE) | |
function rotationToDirectionDegrees(rotation) { | |
const z = rotation.z * (Math.PI / 180.0) | |
const x = rotation.x * (Math.PI / 180.0) | |
const num = Math.abs(Math.cos(x)) | |
return new alt.Vector3( | |
(-Math.sin(z) * num), | |
(Math.cos(z) * num), | |
Math.sin(x), | |
) | |
} | |
function distanceDegrees(a, b) { | |
let dist = Math.abs(a % 360 - b % 360) | |
dist = Math.min(dist, 360 - dist) | |
return dist | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import alt from "alt-server" | |
alt.onClient("sync-player-look-at", (player, posString) => { | |
player.setStreamSyncedMeta("sync-player-look-at", posString) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment