Move camera next to the player
#1

Hi!

All right, so.. here's the code to set the camera right behind the player:

pawn Код:
stock SetCameraNextToPlayer(playerid)
{
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
   
    SetPlayerCameraLookAt(playerid, X, Y, Z);
   
    GetXYInFrontOfPlayer(playerid, X, Y, -3.0);
    SetPlayerCameraPos(playerid, X, Y, Z);
    return true;
}
But I also want to move camera NEXT to the player after setting in behind him, so here's the code to move the camera:
pawn Код:
InterpolateCameraPos(playerid, X, Y, Z, X + 1.0, Y, Z, 2000);
But it doesn't do a trick, because sometimes camera is moving different direction (i.e. left, when it should move right). What else do I have to count, player's angle or something? Actually I don't know.

Thanks!
Reply
#2

Dont Bump the thread 2 times in 1 day.
Reply
#3

I know the rules, but it was 2nd & 3rd page and I really need it, it's serious for me.
Reply
#4

Of course, you must keep in mind the angles, because X and Y doesn't change depending on player's facing angle. I'm going to make some tests to see what can I get.
Reply
#5

Quote:
Originally Posted by [DOG]irinel1996
Посмотреть сообщение
Of course, you must keep in mind the angles, because X and Y doesn't change depending on player's facing angle. I'm going to make some tests to see what can I get.
Would be great mate to receive any results from you. Shit jus' annoys me right now. Thanks for trying
Reply
#6

This is a code I have just quickly whipped up:
pawn Код:
stock SetCameraNextToPlayer(playerid, Float:distance, Float:offset)
{
    new Float:x, Float:y, Float:z, Float:a;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, a);
    new Float:newx = x + (distance * floatsin(-a, degrees));
    new Float:newy = y + (distance * floatcos(-a, degrees));
    //We now have the distance in front of the player.
    SetPlayerCameraPos(playerid, newx, newy, z); //Set the camera behind or in front of the player. ('distance'... Behind = Negative, In front = Positive)
    x = newx, y = newy;
    a -= 90.0;
    newx += floatmul(floatsin(-a, degrees), offset);
    newy += floatmul(floatcos(-a, degrees), offset);
    InterpolateCameraPos(playerid, x, y, z, newx, newy, z, 2000, CAMERA_MOVE); //Move the camera to beside the player. ('offset'... Left = Negative, Right = Positive)
    return 1;
}
Basically, distance is the distance from the player. A positive distance means in front of the player. A negative distance means behind the player.
For example: SetCameraNextToPlayer(playerid, 2, ...) will set the camera in front of the player. SetCameraNextToPlayer(playerid, -2, ...) will set the camera behind the player.
'offset' works in the same way. This is the distance of how far away you want the camera from the player in terms of the 'sideways' length.
For example: SetCameraNextToPlayer(playerid, ..., 2) will set the camera to the right of the player. SetCameraNextToPlayer(playerid, ..., -2) will set the camera to the left of the player.

--
This command is an example of how you would use it:
pawn Код:
CMD:setcamera(playerid, params[])
{
    new Float:distance, Float:offset;
    if(sscanf(params, "ff", distance, offset)) return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /setcamera [distance] [offset]");
    SetCameraNextToPlayer(playerid, distance, offset);
    return 1;
}
Reply
#7

Well.. basically, that's the code I have done yesterday (actually today nearly 3a.m.)

pawn Код:
stock SetCameraNextToPlayer(playerid)
{
    new Float:X, Float:Y, Float:Z, Float:Ang;
    GetPlayerPos(playerid, X, Y, Z);
    GetPlayerFacingAngle(playerid, Ang);
   
    SetCameraBehindPlayer(playerid);
    InterpolateCameraLookAt(playerid, X, Y, Z, X + floatcos(Ang, grades), Y + floatsin(Ang, grades) , Z, 1000);

    X += (-3.0 * floatsin(-Ang, degrees));
    Y += (-3.0 * floatcos(-Ang, degrees));
   
    InterpolateCameraPos(playerid, X, Y, Z, X + floatcos(Ang, grades), Y + floatsin(Ang, grades), Z, 1000);
    return 1;
}
And it's doing a thing I wanted, but your seems to be even more what I wanted. Thanks for that, anyway, I have never done Trigonometry in my life before (I left my country, and in UK they didn't teach me that yet) so basically, I didn't know how to do it. I appreciate your effort you put in this and your message and everything.

Thanks again, I own you one : )

P.S
Just a tip, you must set camera behind player before using InterpolateCameraPos, otherwise, it will go different way. Test your code without SetCameraBehindPlayer and with it.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)