| 123456789101112131415161718192021222324252627282930313233343536 |
- using UnityEngine;
- public class Target : MonoBehaviour
- {
- public Vector3 pointA, pointB, targetDestination;
- public float speed = 2f;
- public AudioSource wasHit;
- public void TakeDamage()
- {
- if (GameManager.gameManager != null)
- {
- GameManager.gameManager.AddPoints();
- wasHit.Play();
- transform.position = transform.position + new Vector3(Random.Range(-5, 5), 0, 0);
- }
- //Destroy(gameObject);
- }
- void Start()
- {
- pointA = transform.position + new Vector3(-5, 0, 0);
- pointB = transform.position + new Vector3(5, 0, 0);
- targetDestination = pointB;
- }
- void Update()//Move left and right
- {
- transform.position = Vector3.MoveTowards(transform.position, targetDestination, speed * Time.deltaTime);
- if (Vector3.Distance(transform.position, targetDestination) < 0.1f)
- {
- targetDestination = (targetDestination == pointA) ? pointB : pointA;
- }
- }
- }
|