Advertisement
Guest User

Unity Javascript Weeping Angel

a guest
Aug 19th, 2012
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. #pragma strict
  2.  
  3. /*
  4. COPYRIGHT 2012 HARRISON TURTON, RELEASED UNDER CREATIVE COMMONS, USEAGE ALLOWED IF YOU CREDIT HARRISON TURTON
  5. */
  6.  
  7. //target to follow
  8. var target : Transform;
  9.  
  10. //Checks his position, used to follow the target
  11. var pos : Transform;
  12.  
  13. //Ray variables (Length... etc.)
  14. var rayLength : float = 3;
  15.  
  16. //Movement, speed etc.
  17. var speed : float = 2;
  18.  
  19. //You can move if he is not being looked at
  20. var move : boolean = false;
  21.  
  22. //I had problems with my model sinking into the floor, adjust this variable if you need it, or remove it
  23. function FixedUpdate()
  24. {
  25. transform.position.y = 2;
  26. }
  27.  
  28. function Update()
  29. {
  30.     //Setting up Raycast variables for simple object avoidance
  31.     var fwd = transform.TransformDirection (Vector3.forward);
  32.     var hit : RaycastHit;
  33.    
  34.     //If you are looking at the object...
  35.     if (renderer.isVisible)
  36.     {
  37.     move = false;
  38.     }
  39.    
  40.     //If you are NOT looking at the object...
  41.     if(!renderer.isVisible)
  42.     {
  43.     move = true;
  44.     }
  45.  
  46.     //If you are not looking at the object...
  47.     if(move)
  48.     {
  49.     //Make him look at the target
  50.     transform.LookAt(target);
  51.     //Always follow the target
  52.     pos.position += pos.forward * speed * Time.deltaTime;
  53.     }
  54.    
  55.     //If he is 3 units away from something, move right (Works if you are not looking at the object)
  56.     if (Physics.Raycast (transform.position, fwd, rayLength) && move)
  57.     {
  58.     Debug.Log("Something ahead, moving");
  59.     transform.Translate(Vector3.right * 3 * Time.deltaTime);
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement