Daniel_Adi_Setiawan

Actor2D

Feb 23rd, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class Actor2D : MonoBehaviour
  7. {
  8. [SerializeField]
  9. GameObject followTarget;
  10. [SerializeField]
  11. Animator anim;
  12. [SerializeField]
  13. NavMeshAgent agent;
  14. [SerializeField]
  15. bool isFlying;
  16.  
  17. private void Awake()
  18. {
  19. anim = GetComponent<Animator>();
  20. agent = followTarget.GetComponent<NavMeshAgent>();
  21. }
  22.  
  23. private void Update()
  24. {
  25. if(!isFlying)
  26. {
  27. anim.SetBool("IsWalking",agent.velocity == Vector3.zero ? false : true);
  28. if(Mathf.Abs(agent.velocity.z) >= Mathf.Abs(agent.velocity.x))
  29. {
  30. anim.SetFloat("TargetZ", -agent.velocity.z);
  31. anim.SetFloat("TargetX", 0);
  32. }
  33. else
  34. {
  35. anim.SetFloat("TargetX", agent.velocity.x);
  36. anim.SetFloat("TargetZ", 0);
  37. }
  38. }
  39. }
  40.  
  41. private void LateUpdate()
  42. {
  43. if(followTarget != null)
  44. {
  45. transform.localPosition = new Vector3(followTarget.transform.localPosition.x, followTarget.transform.localPosition.y, followTarget.transform.localPosition.z);
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment