Basic 3rd person camera script

Posted By: bstudio

Basic 3rd person camera script - 04/07/07 19:17

this is by no means an advanced 3rd person camera script, but should get you started with the camera stuff. Any questions can be mailed to:
bas[dot]tuijnman[at]gmail.com

controls:
- just moving the mouse turns the player (and the camera)
- moving the mouse while holding the left button turns the camera
- turning the mouse wheel adjusts camera height

Here it is:
Code:

//////////////////////////////
//Camera script //
//Written by Bas Tuijnman //
//All rights reserved //
//////////////////////////////
var cam_height = 75;
var cam_height_min = 50;
var cam_height_max = 100;
var cam_behind = 75;
var cam_tilt = -45;
var cam_angle = 0;
var cam_trace;

view 3rd_person //register a new view
{
layer = 1; //place it on layer 1
pos_x = 0; //init on x pos 0
pos_y = 0; //init on y pos 0
}

function camera_init() //initialize our camer
{
camera.visible = off; //turn the standard camera off
3rd_person.size_x = screen_size.x; //make it the width of our screen
3rd_person.size_y = screen_size.y; //make it the height of our screen
3rd_person.genius = player; //attach it to the player
3rd_person.visible = on; //and turn it on
}

function check_cam() //make sure it can't go through walls
{
my = player; //i'm the player
cam_trace = c_trace(player.x,3rd_person.x,IGNORE_ME | IGNORE_PASSABLE); //trace for walls
if(cam_trace==0){return; } //if we didn't hit anything we gan go on
while(cam_trace > 0) //if we did hit something
{
3rd_person.x = player.x + cos(cam_angle)*cam_behind; //move it back
3rd_person.y = player.y + sin(cam_angle)*cam_behind; //move it back
wait(1); //wait one frame to exclude infinite loops
}
}

function control_cam() //make sure we can control it
{
while(player != null) //while the player exists
{
//place camera behind and above player
3rd_person.x = player.x - cos(cam_angle)*cam_behind;
3rd_person.y = player.y - sin(cam_angle)*cam_behind;
3rd_person.z = player.z + cam_height;

//and give it the appropriate angles
3rd_person.tilt = cam_tilt;
3rd_person.roll = player.roll;
3rd_person.pan = cam_angle;

if(mouse_force.x < 0 && mouse_right == 1) //if the mouse goes to the left and we have the right mouse button pressed
{
cam_angle -= pl_speed * time_step; //turn the camera left
}
if(mouse_force.x > 0 && mouse_right == 1) //if the mouse goes to the right and we have the right mouse button pressed
{
cam_angle += pl_speed * time_step; //turn the camera
}
if(mouse_force.x < 0 && mouse_right == 0) //if the mouse goes to the left and we don't have the right mouse button pressed
{
player.pan -= pl_speed * time_step; //turn the player
}
if(mouse_force.x > 0 && mouse_right == 0) //if the mouse goes to the right and we don't have the right mouse button pressed
{
player.pan += pl_speed * time_step; //turn the player
}
if(mickey.z > 0 && cam_height < cam_height_max) //if we turn the mouse wheel down
{
cam_height += pl_speed * time_step; //move camera up
cam_behind += pl_speed * time_step; //and more backwards
}
if(mickey.z < 0 && cam_height > cam_height_min) //if we turn the mouse wheel up
{
cam_height -= pl_speed * time_step; //move camera down
cam_behind -= pl_speed * time_step; //and more forward
}
check_cam(); //make sure we don't hit walls
wait(1); //and exclude infinite loops
}
}



to use, include this file and do this in your main script:
Code:

function main()
{
load_level("your_level.wmb");
camera_init();
wait(1);
}

action your_player_action
{
player = me;
control_cam();
}



Use it freely, credit is not needed but will be appreciated

cheers
Posted By: frazzle

Re: Basic 3rd person camera script - 04/08/07 09:07

Thxn, camera contributions are always welcome

Happy Easter ^^

Frazzle
Posted By: raiden

Re: Basic 3rd person camera script - 04/15/07 12:07

Thank you bstudio. Nice contribution and very well written.

-raiden
Posted By: bstudio

Re: Basic 3rd person camera script - 04/15/07 12:55

No problem
© 2024 lite-C Forums