Target.cs 1011 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. public class Target : MonoBehaviour
  3. {
  4. public Vector3 pointA, pointB, targetDestination;
  5. public float speed = 2f;
  6. public AudioSource wasHit;
  7. public void TakeDamage()
  8. {
  9. if (GameManager.gameManager != null)
  10. {
  11. GameManager.gameManager.AddPoints();
  12. wasHit.Play();
  13. transform.position = transform.position + new Vector3(Random.Range(-5, 5), 0, 0);
  14. }
  15. //Destroy(gameObject);
  16. }
  17. void Start()
  18. {
  19. pointA = transform.position + new Vector3(-5, 0, 0);
  20. pointB = transform.position + new Vector3(5, 0, 0);
  21. targetDestination = pointB;
  22. }
  23. void Update()//Move left and right
  24. {
  25. transform.position = Vector3.MoveTowards(transform.position, targetDestination, speed * Time.deltaTime);
  26. if (Vector3.Distance(transform.position, targetDestination) < 0.1f)
  27. {
  28. targetDestination = (targetDestination == pointA) ? pointB : pointA;
  29. }
  30. }
  31. }