I was recently asked for a simple movement script to start someone off. Since I had one lying around, I decided might as well share it here as well. This script works perfectly fine with model-model collisions since it's set up to use all the "c_" instructions properly. To use it, simply copy and paste this code at the bottom of your main script and apply the "playerdef" action to a dummy model in WED. This action then creates a model, rotates it according to the dummy model, then starts the movement. Be sure to check the playerdef action and place your own model name in the ent_create().
You may notice your model or the camera is in inversed directions, if this happens then simply turn your model 180 degrees in MED and save it again. Or, just change a few things in the script. When I created the script, I believe the model I tested with was pointed in the -x direction...
Code:
define tracekart, skill70;
define z_normal, skill71;
define g_type, skill72;
define height, skill73;
define z_target, skill74;
define maxTorque, skill75;
function ccamera()
{
var cam_dist = 320; //how far away?
var cam_hi = 100; //how high up?
var cam_changa[3];
var tilt_3rd = 16; //tilt the camera
while(1)
{
temp.x = -cam_dist;
temp.y = 0;
temp.z = 0;
vec_set(cam_changa.pan, my.pan);
vec_rotate(temp,cam_changa.pan);
vec_add(temp,my.x);
temp.z = my.z + cam_hi;
vec_set(camera.x, temp.x);
camera.pan = cam_changa.pan;
camera.roll = 0;
camera.tilt = - tilt_3rd;
wait(1);
}
}
//-------------------------movement-----------------------------------
function move_player()
{
var anormal[3];
var g_normal[3];
var f_normal[3];
var align_kart = 0.7; //how quickly the kart re-aligns with the ground normals
var move[3];
var turn_speed;//pan speed
var run_speed; //x-axis forward speed
var f_speed; //the forward speed constant
var slide_speed; //y-axis side speed
var turn_direction; //determines the negative or positive values of speed
var run_direction;
var slide_direction;
var hill_speed; //Contains the calculated hill velocity
var hill_time; //Contains how long you've been on the hill
var hill_factor; //Contains the slowdown factor from being on a hill
f_speed = 120;
run_speed = f_speed;
slide_speed = 30;
turn_speed = 1;
vec_set(my.pan, you.pan);
my.fat = on;
my.narrow = on;
c_setminmax(my);
my.polygon = off;
move_min_z = -1;
move_friction = 0;
Disable_Z_Glide = 0;
ccamera(); //call camera code
while(1)
{
if(key_a){turn_direction = 1;} if(key_d){turn_direction = -1;}
if(!key_a && !key_d) || (key_a == 1 && key_d == 1){turn_direction = 0;}
if(key_w){run_direction = 1;} if(key_s){run_direction = -1;}
if(key_s && key_w) || (key_s == 0 && key_w == 0) {run_direction = 0;}
if(key_shift && key_d){slide_direction = 1;} if(key_shift && key_a){slide_direction = -1;}
if(key_shift == 1) && (key_a == 1 && key_d == 1) || (key_shift == 0) && (key_a == 0 || key_d == 0){slide_direction = 0;}
vec_set(temp, my.x); temp.z -= 4000;
result = c_trace(my.x, temp.x, ignore_me+ignore_sprites+ignore_passents+ignore_passable+use_box);
my.z_normal = normal.z;
if(my.z_normal == 0){my.z_normal = 0.01;}
my.height = result;
if(target)//Find ground angle from normal
{
my.z_target = target.z;
if((normal.x != 0) || (normal.y != 0))
{
vec_set(anormal.pan ,nullvector);
anormal.pan = -my.pan;
vec_rotate(normal,anormal.pan);
anormal.tilt = -asin(normal.x);
anormal.roll = -asin(normal.y);
}
else
{
anormal.tilt = 0;
anormal.roll = 0;
}
}
hill_time = 3/my.z_normal;
if(hill_factor < hill_time){hill_factor += 1/my.z_normal * time;}else{hill_factor = 3/my.z_normal;}
if(hill_factor < 0){hill_factor = 3/my.z_normal;}
my.maxtorque = 2;
hill_speed = my.tilt * run_direction * hill_factor/my.maxtorque;
if(my.roll > 40){slide_direction = -2; if(turn_direction == -1){turn_direction = 0.2;}}
if(my.roll < -40){slide_direction = 2; if(turn_direction == 1){turn_direction = -0.2;}}
run_speed = f_speed - hill_speed;
move.tilt = ang(anormal.tilt - my.tilt) * 1 * time; //turn player according to calculated ground angle over time
move.roll = ang(anormal.roll - my.roll) * 1 * time;
move.pan = turn_direction * 4 * time;
c_rotate(my,move.pan,ignore_passable + ignore_passents + ignore_push + glide);
if(my.height > 40) //if the player is still in the air, move the player down
{
move.x = 0; //running speed
move.y = 0; //turn speed
move.z = -25 * time;
}
else
{
move.z = 0; // distance down
if(my.z_normal > 0.4)
{
vec_set(temp.x, vector(0,0,40)); temp.z = my.z_target + temp.z; temp.z = my.z - temp.z; temp.z = -temp.z;
c_move(my,nullvector,temp.x,ignore_me + ignore_passable + glide + ACTIVATE_TRIGGER);
}
}
move.x = run_speed * run_direction * time;
move.y = slide_speed * slide_direction * time;
vec_rotate(move.x, my.pan);
c_move(my,nullvector,move,ignore_passable + glide); //move using absolute coordinates
wait(1);
}
}
action playerdef
{
my.passable = on;
my.invisible = on;
ent_create("player.mdl", my.x, move_player);
}