Hi everyone, can anyone please change this first person camera script and make it into a 3rd person camera script.Please can anyone help,here is the script
///////////////////////////////////////////////////////////////////////////////////////
var camMode = -1; // 0:orbit, 1:chase, 2:cockpit, -1 disabled
var rotspd = 20; // cam movement speed
var minang = 5; // orbit between 5-70 degrees and 200-3000 quants
var maxang = 70;
var mindist = 200;
var maxdist = 3000;
var cameraTPos[3]; // camera target position
var cameraTAng[3]; // camera target angle
var temp[3];
var temp2[3];
var vecChaseOffset[3]= {-150,0,60}; // position of the camera in relation to the chassis center (chase)
var vecDriverOffset[3]= {-35,10,34}; // position of the camera in relation to the chassis center (cockpit)
var linSpeed; // car speed in XY
///////////////////////////////////////////////////////////////////////////////////////
#define KEY_CAMERA key_c // press "C" to go through the available camera modes
#define JOY_CAMERA joy_3
///////////////////////////////////////////////////////////////////////////////////////
ANGLE vecChaseAngOff; // angle of the chase camera
///////////////////////////////////////////////////////////////////////////////////////
ENTITY* pFocus; // the camera will orbit around this entity
///////////////////////////////////////////////////////////////////////////////////////
function ControlCamera_startup()
{
camera.fog_start = 1000;
camera.fog_end = 1.2 * camera.clip_far;
camera.clip_near = 25;
// init the level and wait for the focus object (we will orbit around it)
while(pFocus == NULL || camMode < 0) {wait(1);}
var cam_dist=200;
ANGLE cam_ang;
cam_ang.pan = pFocus.pan - 180;
cam_ang.tilt = 30;
cam_ang.roll = 0;
while(1)
{
while(pFocus == NULL) {wait(1);} // wait for the focus entity
if (camMode == 0) // free orbit camera?
{
reset(pFocus,INVISIBLE);
if(mouse_right) // press and hold the right mouse button to rotate the camera around
{
cam_ang.pan += rotspd * mouse_force.x;
cam_ang.tilt += rotspd * mouse_force.y;
cam_ang.tilt = clamp(cam_ang.tilt, minang, maxang);
}
cam_dist -= integer(mickey.z); // zoom using the mouse wheel
cam_dist = clamp(cam_dist, mindist, maxdist);
// find the camera location and then aim it at pFocus
camera.x = pFocus.x + cos(cam_ang.pan) * (cam_dist * cos(cam_ang.tilt));
camera.y = pFocus.y + sin(cam_ang.pan) * (cam_dist*cos(cam_ang.tilt));
camera.z = pFocus.z + sin(cam_ang.tilt) * cam_dist;
vec_set(temp,pFocus.x);
vec_sub(temp,camera.x);
vec_to_angle(camera.pan,temp);
}
if (camMode==1) // chase camera?
{
reset(pFocus,INVISIBLE);
if(mouse_right)
{
vecChaseAngOff.pan -= rotspd * mouse_force.x * time_step;
vecChaseAngOff.tilt += rotspd * mouse_force.y * time_step;
vecChaseAngOff.pan = clamp(vecChaseAngOff.pan, -20, 20);
vecChaseAngOff.tilt = clamp(vecChaseAngOff.tilt, -20, 20);
}
else
{
// return to center
if(vecChaseAngOff.pan > 0.25) {vecChaseAngOff.pan -= 2 * time_step; }
if(vecChaseAngOff.pan < -0.25) {vecChaseAngOff.pan += 2 * time_step; }
if(vecChaseAngOff.tilt > 6) {vecChaseAngOff.tilt -= 2 * time_step; }
if(vecChaseAngOff.tilt < 5) {vecChaseAngOff.tilt += 2 * time_step; }
}
vec_set(cameraTPos, vecChaseOffset); // move behind and upwards
var offset;
offset = clamp(0.01 * linSpeed * linSpeed, -100, 100);
cameraTPos[0] -= offset; // offset by speed
vec_rotate(cameraTPos, vector(ang(pFocus.pan), 0, 0));
vec_add(cameraTPos, pFocus.x);
// collide with any object in the way
c_trace(pFocus.x,cameraTPos, (IGNORE_PASSABLE+IGNORE_PASSENTS+IGNORE_MODELS));
if(trace_hit == 1)
{
// offset from the target along the vector
vec_set(cameraTPos,target);
// offset from the wall along normal by the near clipping dist
vec_normalize(normal,(camera.clip_near / 2));
vec_add(cameraTPos, normal);
}
else
{
// make sure we don't clip into walls nearby
vec_diff(temp,pFocus.x, cameraTPos);
vec_normalize(temp, camera.clip_near);
vec_rotate(temp, vector(90,0,0));
vec_set(temp2, cameraTPos);
vec_add(temp2, temp); // move out by the clip_near value
c_trace(cameraTPos, temp2, (IGNORE_PASSABLE+IGNORE_PASSENTS+IGNORE_MODELS));
if(trace_hit == 1)
{
// offset from that target along the vector
vec_set(cameraTPos, target);
// offset from the wall along normal by the near clipping dist
vec_normalize(normal, (camera.clip_near / 2));
vec_add(cameraTPos,normal);
}
else
{
// rotate temp back the other dir twice as much..
vec_rotate(temp, vector(-180, 0, 0));
vec_set(temp2, cameraTPos);
vec_add(temp2, temp); // move out by the clip_near value
c_trace(cameraTPos, temp2, (IGNORE_PASSABLE+IGNORE_PASSENTS+IGNORE_MODELS));
if(trace_hit == 1)
{
// offset from that target along the vector
vec_set(cameraTPos, target);
// offset from the wall along normal by the near clipping dist
vec_normalize(normal, (camera.clip_near / 2));
vec_add(cameraTPos, normal);
}
}
}
// smooth out the transition
vec_diff(temp, cameraTPos, camera.x);
var vscale = clamp((time_step * 0.5), 0.001, 1);
vec_scale(temp, vscale);
vec_add(camera.x, temp);
// calc angle from camera to target...
vec_diff(temp, pFocus.x, cameraTPos);
vec_to_angle(cameraTAng, temp); // camera target angle looks at focus center
vec_add(cameraTAng, vecChaseAngOff.pan); // tweak it
// smooth out the rotation
vec_diff(temp, cameraTAng, camera.pan);
temp[0] = ang(temp[0]); // we only need the pan angle
vec_scale(temp, vscale);
vec_add(camera.pan, temp);
}
if (camMode == 2) // cockpit view?
{
vec_set(camera.pan, pFocus.pan);
vec_set(camera.x, pFocus.x);
// move to the driver side
vec_set(temp, vecDriverOffset);
vec_rotate(temp, pFocus.pan);
vec_add(camera.x, temp);
}
// toggle the camera
if (KEY_CAMERA || JOY_CAMERA)
{
while (KEY_CAMERA || JOY_CAMERA) {wait(1);} // wait until the key is released
camMode +=1; if (camMode > 2) {camMode = 0;}
// reset if previously rolled around
vec_set(camera.pan, cam_ang);
}
wait(1);
}
}