Ghost Moving Script problem
my ghost direction is random.value using to move on the plane.
i need the ghost script to detect the wall hit after choose the direction to move.
i want to create like that ghost not come back from the starting position.
my code is there pls other…
Random.onUnitSphere;// it will get only randon value.
i need it will automatically choose the position to move
function Update()
{
if (Time.time > nextUpdate) {
nextUpdate = Time.time + (Random.value * howLong);
direction =Random.onUnitSphere;
direction.y = 0;
direction.Normalize ();
direction *= howFast;
direction.y = 1.5 - transform.position.y;
}
var controller = GetComponent(CharacterController);
controller.Move(direction * Time.deltaTime);
}
Look Here.
It is a good tutorial on creating basic AI on top of the CharacterController script.
i found the enemy direction move with waypoint
var waypoint1:Transform[];
// The amount of Waypoint you want
var waypoint2:Transform;
var waypoint3:Transform;
var waypoint4:Transform;
var waypoint5:Transform;
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var player : Transform; // Referance to the Player
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
var attackRange = 10; // Range to start the attack
var attackSpeed = 5.0; // Speed to attack
var attackLook = 10.0; // How fast to turn when attacking
var enemy_collider:boolean=true;
var LifeFive:Texture2D;
var LifeFour:Texture2D;
var LifeThree:Texture2D;
var LifeTwo:Texture2D;
var LifeOne:Texture2D;
private var distanceToPlayer : int; // Distance from the enemy to the Player
private var distanceToWaypoint : int;
private var curTime : float;
private var currentWaypoint : int = 0;
private var temp_currentWaypoint : int;
private var character : CharacterController;
private var gravity : float = 2.0;
private var attacking : boolean = false;
private var escape:boolean=false;
private var waypoint:Transform[];
static var count:int=0;
//another script to get the variable values
var penetrate:int=0;
static var flag:EatPellet;
var des:int;
static var lives:int=0;
var a:int;
var ene1:int;
var ene2:int;
var ene3:int;
var ene4:int;
var ene5:int;
var name_data:String;
var enemy:GameObject;
var go:GameObject[];
var Level_check:String;
function Awake(){
}
function Start(){
player=GameObject.FindGameObjectWithTag("Player").transform;
waypoint=waypoint1;
character = GetComponent(CharacterController);
if(penetrate==0)
{
yield WaitForSeconds(5);
penetrate=1;
}
}
function Update(){
go=GameObject.FindGameObjectsWithTag("enemy");
des=flag.eat;
distanceToPlayer = Vector3.Distance(player.position, transform.position); // Distance between the enemy and the Player
Level_check=Application.loadedLevelName;
if(Level_check=="Level1")
{
print(Level_check);
}
if(Level_check=="Level2")
{
print(Level_check);
}
if(distanceToPlayer < attackRange && des==0){
attacking = true;
attack();
}
else{
attacking = false;
}
if(currentWaypoint < waypoint.length && !attacking){
patrol();
}
else if(currentWaypoint == waypoint.length && !attacking)
{
var a:int=Random.Range(1,5);
if(a==1)
{
waypoint=waypoint2;
currentWaypoint=0;
}
if(a==2)
{
waypoint=waypoint3;
currentWaypoint=0;
}
if(a==3)
{
waypoint=waypoint4;
currentWaypoint=0;
}
if(a==4)
{
waypoint=waypoint5;
currentWaypoint=0;
}
}
else if(attacking)
{
currentWaypoint=temp_currentWaypoint;
}
else
{
currentWaypoint=0;
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
//distanceToWaypoint=Vector3.Distance(target, transform.position);
// print(distanceToWaypoint);
if(moveDirection.magnitude < 0.5){
if (curTime == 0)
curTime = Time.time; // Pause over the Waypoint
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}
else{
// Look at and dampen the rotation
//var rotation = Quaternion.LookRotation(target - transform.position);
//transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
function attack(){
// Attack the Player
//Rotate to face the Player
//var lookPos = player.position - transform.position;
//lookPos.y = 0;
//var rotation = Quaternion.LookRotation(lookPos);
//transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
// Move towards the Player
if(attacking)
{
temp_currentWaypoint=currentWaypoint;
var target : Vector3 = player.position;
var moveDirection : Vector3 = target - transform.position;
// moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
}
}