Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var speed:float;
- private var controller:CharacterController;
- private var moveDirection:Vector3;
- private var delayRotation:float;
- private var changeRotation:float;
- private var newRotation:float;
- private var hit:RaycastHit;
- private var dirToMain:Vector3;
- private var fight;
- function Start () {
- delayRotation = Random.Range(1,6);
- newRotation = Random.Range(-360,361);
- controller = GetComponent("CharacterController");
- }
- function Update () {
- //Direction vers le personnage
- dirToMain = GameObject.Find("Skull").transform.position + transform.position;
- dirToMain.y = 0;
- if(!fight){
- if(dirToMain.magnitude < 100){
- //Suivre le vecteur dirToMain qui va de l'ennemi a Skull
- moveDirection = dirToMain * 0.5;
- transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dirToMain),6 * Global.deltaTime);
- }else{
- //Mouvement Autonome
- //Vecteur directeur
- moveDirection = Vector3.forward * speed;
- //Changement de rotation permanent
- if(changeRotation + delayRotation < Global.fixedTime){
- newRotation = Random.Range(-360,361);
- changeRotation = Global.fixedTime;
- delayRotation = Random.Range(1,6);
- }
- //Test du raycast
- if(Physics.Raycast(transform.Find("origin").position,transform.forward,hit)){
- if(hit.distance < 30){
- transform.rotation = Quaternion.Slerp(transform.rotation,transform.rotation * Quaternion.Euler(0,180,0),0.5 * Global.deltaTime);
- }else{
- transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(0,newRotation,0),0.5 * Global.deltaTime);
- }
- }
- //Transformation local
- moveDirection = transform.TransformDirection(moveDirection);
- }
- }
- //Fight
- if(dirToMain.magnitude < 10){
- fight = true;
- //On stop le personnage
- moveDirection = Vector3.zero;
- //Mais il continue de regarder Skull
- transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dirToMain),6 * Global.deltaTime);
- }else{
- fight = false;
- }
- //Animation
- if(fight){
- transform.Find("skeletonDark").animation.CrossFade("attack",0.5 * Global.deltaTime);
- }else{
- transform.Find("skeletonDark").animation.CrossFade("run",0.5 * Global.deltaTime);
- }
- //Deplacement Ennemi
- moveDirection.y -= Global.gravity;
- controller.Move(moveDirection * Global.deltaTime);
- }
- //Collision objet
- function OnControllerColliderHit(hit:ControllerColliderHit){
- if(hit.transform.name != "Terrain"){
- transform.rotation = Quaternion.Slerp(transform.rotation,transform.rotation * Quaternion.Euler(0,180,0),0.5 * Global.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement