Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ped Edit Camera
import * as alt from 'alt';
import * as native from 'natives';
let cameraControlsInterval;
let camera;
let zpos = 0;
let fov = 90;
let startPosition;
let startCamPosition;
let timeBetweenAnimChecks = Date.now() + 100;
export function createPedEditCamera() {
startPosition = { ...alt.Player.local.pos };
if (!camera) {
const forwardVector = native.getEntityForwardVector(alt.Player.local.scriptID);
const forwardCameraPosition = {
x: startPosition.x + forwardVector.x * 1.2,
y: startPosition.y + forwardVector.y * 1.2,
z: startPosition.z + zpos
};
fov = 90;
startCamPosition = forwardCameraPosition;
camera = native.createCamWithParams(
'DEFAULT_SCRIPTED_CAMERA',
forwardCameraPosition.x,
forwardCameraPosition.y,
forwardCameraPosition.z,
0,
0,
0,
fov,
true,
0
);
native.pointCamAtCoord(camera, startPosition.x, startPosition.y, startPosition.z);
native.setCamActive(camera, true);
native.renderScriptCams(true, false, 0, true, false);
}
if (!cameraControlsInterval) {
cameraControlsInterval = alt.setInterval(handleControls, 0);
}
}
export function destroyPedEditCamera() {
if (cameraControlsInterval) {
alt.clearInterval(cameraControlsInterval);
cameraControlsInterval = null;
}
if (camera) {
camera = null;
}
native.destroyAllCams(true);
native.renderScriptCams(false, false, 0, false, false);
}
function handleControls() {
native.disableControlAction(0, 24, true);
native.disableControlAction(0, 25, true);
native.disableControlAction(0, 32, true); // w
native.disableControlAction(0, 33, true); // s
native.disableControlAction(0, 34, true); // a
native.disableControlAction(0, 35, true); // d
const [_, width] = native.getActiveScreenResolution(0, 0);
const cursor = alt.getCursorPos();
const _x = cursor.x;
let oldHeading = native.getEntityHeading(alt.Player.local.scriptID);
// SCroll Up
if (native.isDisabledControlPressed(0, 15)) {
if (_x < width / 2 + 250 && _x > width / 2 - 250) {
fov -= 2;
if (fov < 10) {
fov = 10;
}
native.setCamFov(camera, fov);
native.setCamActive(camera, true);
native.renderScriptCams(true, false, 0, true, false);
}
}
// SCroll Down
if (native.isDisabledControlPressed(0, 16)) {
if (_x < width / 2 + 250 && _x > width / 2 - 250) {
fov += 2;
if (fov > 130) {
fov = 130;
}
native.setCamFov(camera, fov);
native.setCamActive(camera, true);
native.renderScriptCams(true, false, 0, true, false);
}
}
if (native.isDisabledControlPressed(0, 32)) {
zpos += 0.01;
if (zpos > 1.2) {
zpos = 1.2;
}
native.setCamCoord(camera, startCamPosition.x, startCamPosition.y, startCamPosition.z + zpos);
native.pointCamAtCoord(camera, startPosition.x, startPosition.y, startPosition.z + zpos);
native.setCamActive(camera, true);
native.renderScriptCams(true, false, 0, true, false);
}
if (native.isDisabledControlPressed(0, 33)) {
zpos -= 0.01;
if (zpos < -1.2) {
zpos = -1.2;
}
native.setCamCoord(camera, startCamPosition.x, startCamPosition.y, startCamPosition.z + zpos);
native.pointCamAtCoord(camera, startPosition.x, startPosition.y, startPosition.z + zpos);
native.setCamActive(camera, true);
native.renderScriptCams(true, false, 0, true, false);
}
// rmb
if (native.isDisabledControlPressed(0, 25)) {
// Rotate Negative
if (_x < width / 2) {
const newHeading = (oldHeading -= 2);
native.setEntityHeading(alt.Player.local.scriptID, newHeading);
}
// Rotate Positive
if (_x > width / 2) {
const newHeading = (oldHeading += 2);
native.setEntityHeading(alt.Player.local.scriptID, newHeading);
}
}
// d
if (native.isDisabledControlPressed(0, 35)) {
const newHeading = (oldHeading += 2);
native.setEntityHeading(alt.Player.local.scriptID, newHeading);
}
// a
if (native.isDisabledControlPressed(0, 34)) {
const newHeading = (oldHeading -= 2);
native.setEntityHeading(alt.Player.local.scriptID, newHeading);
}
if (Date.now() > timeBetweenAnimChecks) {
timeBetweenAnimChecks = Date.now() + 1500;
if (!native.isEntityPlayingAnim(alt.Player.local.scriptID, 'nm@hands', 'hands_up', 3)) {
alt.emit('animation:Play', {
dict: 'nm@hands',
name: 'hands_up',
duration: -1,
flag: 2
});
}
}
}
@zerefdev

This comment has been minimized.

Copy link

@zerefdev zerefdev commented Apr 20, 2020

https://natives.altv.mp/#/0xB13C14F66A00D047
L77 :

if (fov<1) fov = 1;

L87 :

if (fov>130) fov = 130;

To prevent a behavior where after you reach the fov limit and you keep zooming, later when you want to zoom the other direction it doesn't zoom until it's below/above the limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment