Tuesday, 16 April 2013

Possible Following A.I. code?

 I found this on UnityAnswers, its originally for an enemy to follow player but it doesn't have any triggers in it its simply the following aspect of the script. It's Java.

  1. var target : Transform; //the target
  2. var moveSpeed = 3;
  3. var rotationSpeed = 3; //speed of turning
  4.  
  5. var myTransform : Transform; //current transform data of A.I.
  6.  
  7. function Awake()
  8. {
  9. myTransform = transform;
  10. }
  11.  
  12. function Start()
  13. {
  14. target = GameObject.FindWithTag("Player").transform; //target the player
  15.  
  16. }
  17.  
  18. function Update () {
  19. //rotate to look at the player
  20. myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
  21. Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  22.  
  23. //move towards the player
  24. myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  25.  
  26.  
  27. } from what I can gather so long as we have the firstperson camera tagged player (which i think it is) we attach this code to the A.I. and it should follow no matter how far away the player is. Also this was on UnifyCommunity wiki. .http://wiki.unity3d.com/index.php/Flocking

No comments:

Post a Comment