Advertisement
Guest User

ironmaiden30

a guest
Sep 2nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1.  
  2.  
  3. var speed:float;
  4.  
  5. private var controller:CharacterController;
  6. private var moveDirection:Vector3;
  7. private var delayRotation:float;
  8. private var changeRotation:float;
  9. private var newRotation:float;
  10. private var hit:RaycastHit;
  11. private var dirToMain:Vector3;
  12. private var fight;
  13.  
  14.  
  15. function Start () {
  16.     delayRotation = Random.Range(1,6); 
  17.     newRotation = Random.Range(-360,361);
  18.     controller = GetComponent("CharacterController");
  19.  
  20. }
  21.  
  22. function Update () {
  23.     //Direction vers le personnage
  24.     dirToMain = GameObject.Find("Skull").transform.position + transform.position;
  25.     dirToMain.y = 0;
  26.    
  27.     if(!fight){
  28.         if(dirToMain.magnitude < 100){
  29.             //Suivre le vecteur dirToMain qui va de l'ennemi a Skull
  30.             moveDirection = dirToMain * 0.5;
  31.             transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dirToMain),6 * Global.deltaTime);
  32.         }else{
  33.             //Mouvement Autonome
  34.            
  35.             //Vecteur directeur
  36.             moveDirection = Vector3.forward * speed;
  37.            
  38.             //Changement de rotation permanent
  39.             if(changeRotation + delayRotation < Global.fixedTime){
  40.                 newRotation = Random.Range(-360,361);
  41.                 changeRotation = Global.fixedTime;
  42.                 delayRotation = Random.Range(1,6);
  43.             }
  44.             //Test du raycast
  45.             if(Physics.Raycast(transform.Find("origin").position,transform.forward,hit)){
  46.                 if(hit.distance < 30){
  47.                     transform.rotation = Quaternion.Slerp(transform.rotation,transform.rotation * Quaternion.Euler(0,180,0),0.5 * Global.deltaTime);
  48.                 }else{
  49.                     transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.Euler(0,newRotation,0),0.5 * Global.deltaTime);
  50.             }
  51.         }
  52.         //Transformation local
  53.         moveDirection = transform.TransformDirection(moveDirection);   
  54.      }
  55.      
  56.   }
  57.     //Fight
  58.      if(dirToMain.magnitude < 10){
  59.         fight = true;
  60.        
  61.         //On stop le personnage
  62.         moveDirection = Vector3.zero;
  63.         //Mais il continue de regarder Skull
  64.         transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dirToMain),6 * Global.deltaTime);
  65.         }else{
  66.             fight = false;
  67.        
  68.     }
  69.    
  70.     //Animation
  71.     if(fight){
  72.         transform.Find("skeletonDark").animation.CrossFade("attack",0.5 * Global.deltaTime);
  73.     }else{
  74.         transform.Find("skeletonDark").animation.CrossFade("run",0.5 * Global.deltaTime);
  75.     }
  76.     //Deplacement Ennemi
  77.     moveDirection.y -= Global.gravity;
  78.     controller.Move(moveDirection * Global.deltaTime);
  79. }
  80. //Collision objet
  81. function OnControllerColliderHit(hit:ControllerColliderHit){
  82.     if(hit.transform.name != "Terrain"){
  83.     transform.rotation = Quaternion.Slerp(transform.rotation,transform.rotation * Quaternion.Euler(0,180,0),0.5 * Global.deltaTime);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement