This tutorials is for beginners to learn coding game character zombie AI in Skyline.
It's a step by step and progressive tutorial, we'll start with very basics and each new tutorial will add new features.
1) Drop "Fps player" object in the scene
2) With "Asset Manager" seek character file in "Models" folder : "Zombie.mesh" and drop it in the scene
3) Add a "Simple character controller" to the zombie
4) Check physics scene debugger and adjust Capsule collision values
5) Crate a Lua file with code below
- Code: Select all
sky.include ( "random.lua" )
obj = 0;
moveMaxSpeed = 5;
interval_change_direction = 1700;
TIMER_DIRECTION = 1;
function onInit(objID)
obj = objID;
character.setGravity(obj, 0, -10, 0);
playerID = entity.getEntityIDFromTag("Player");
-- create a new timer to change direction
time.startTimer(TIMER_DIRECTION, obj, interval_change_direction);
end
function onUpdate( td )
character.move(obj, moveMaxSpeed );
--entity.turn(obj, 0, 60*td, 0)
--getOppositeDirection();
end
function getRandomDirection()
-- get zombie position
direction = newType.vec3(entity.getPosition(obj));
-- get random values
randomX = math.random(10,10);
randomZ = math.random(-10,10);
sky.print(randomX);
sky.print(randomZ);
--add random values to zombie position , used as next position to move
direction.x = direction.x + randomX;
direction.z = direction.z + randomZ ;
--rotate zombie to next position
entity.setDirection(obj,direction.x, 0, direction.z);
end
function onTimer()
-- every time intervall call function to change zombie direction
getRandomDirection() ;
end
6) Attach previous Lua code file to zombie
Press run to see the zombie moving and changing direction.
In this tutorial you learned how to use :
* Simple character controller set up
* "sky.include" needed to use external Lua scripts It looks it's not needed.
* set character gravity
* one simple timer
* Random function usage
* get entity position and Vector 3 manipulation
Later we can extend this tutorials adding animations and using a smooth turn function.
Note: How to change image size on forums