C#C
C#11mo ago
Readable

How to activate Child Class?

// 

public class Weapon: MonoBehaviour
{
    
    {
        public Rigidbody projectile;
        public float speed = 25;
        [SerializeField] private Transform targetCube;
        [SerializeField] private float force;
        [SerializeField] private float rotationForce;
        private Rigidbody rb;

        // Update is called once per frame
        public void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                shoot();
            }
        }

        public void shoot()
        {
            Rigidbody instantiatedProjectile =
                Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
        }
    }

    class Throw : Weapon
    {
        private void Start()
        {
            rb = GetComponent<Rigidbody>();
        }

        private void FixedUpdate()
        {
            if (Input.GetKey(KeyCode.E))
            {
                Vector3 direction = targetCube.position - rb.position;
                direction.Normalize();
                Vector3 rotationAmount = Vector3.Cross(transform.forward, direction);
                rb.angularVelocity = rotationAmount * rotationForce;
                rb.velocity = transform.forward * force;
            }
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Enemy"))
            {
                Destroy(gameObject);
            }
        }
    }
}
Was this page helpful?