Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Bullet : MonoBehaviour
- {
- private Transform target;
- public float speed = 70f;
- public GameObject impactEffect;
- public void Seek(Transform _target)
- {
- target = _target;
- }
- // Update is called once per frame
- void Update()
- {
- if(target == null)
- {
- Destroy(gameObject);
- return;
- }
- Vector3 dir = target.position - transform.position;
- float distanceThisFrame = speed * Time.deltaTime;
- if (dir.magnitude <= distanceThisFrame)
- {
- HitTarget();
- return;
- }
- transform.Translate(dir.normalized * distanceThisFrame, Space.World);
- transform.LookAt(target);
- }
- void HitTarget ()
- {
- GameObject effectIns = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
- Destroy(effectIns, 5f);
- Destroy(target.gameObject);
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement