43 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Collections;
 | 
						|
using System.Collections.Generic;
 | 
						|
using UnityEngine;
 | 
						|
 | 
						|
public class BulletScript : MonoBehaviour
 | 
						|
{
 | 
						|
    public float damage;
 | 
						|
    public GameObject bulletImpact;
 | 
						|
 | 
						|
    private void Start()
 | 
						|
    {
 | 
						|
        AudioManager.instance.Play("Shoot");
 | 
						|
    }
 | 
						|
 | 
						|
    void Update() {
 | 
						|
        Vector2 vel = gameObject.GetComponent<Rigidbody2D>().velocity;
 | 
						|
        gameObject.transform.rotation = Quaternion.Euler(new Vector3(0,0,Mathf.Rad2Deg * Mathf.Atan2(vel.y,vel.x)));
 | 
						|
    }
 | 
						|
 | 
						|
    private void OnCollisionEnter2D(Collision2D collision) {
 | 
						|
        AudioManager.instance.Play("ShootCollide");
 | 
						|
        CameraShake.instance.Shake(.1f, .1f);
 | 
						|
        Instantiate(bulletImpact, transform.position, Quaternion.identity);
 | 
						|
        Button button = collision.transform.GetComponent<Button>();
 | 
						|
        if (!button)
 | 
						|
            Object.Destroy(gameObject);
 | 
						|
        else if(!button.bulletProof)
 | 
						|
        {
 | 
						|
            button.Press();
 | 
						|
            Destroy(gameObject);
 | 
						|
        }
 | 
						|
        hasHP hp;
 | 
						|
        if (collision.gameObject.TryGetComponent<hasHP>(out hp)) {
 | 
						|
            Debug.Log("Damaging");
 | 
						|
            hp.damage(damage);
 | 
						|
        }
 | 
						|
        Debug.Log(collision.otherCollider.gameObject.name);
 | 
						|
        if (collision.otherCollider.gameObject.name.Equals("Player")) {
 | 
						|
            collision.otherCollider.gameObject.GetComponent<PlayerStats>().Die();
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
}
 |