Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Unity Bullet script

    // You need a Firepoint (where the bullet comes from)
    // You need a BulletPrefab to generate a bullet at the Firepoint 
    public Transform FirePoint;
    public GameObject BulletPrefab;
    
    //Decides how fast the bullet shoots
    public float bulletForce = 20f;

    void Update()
    //If the input key is activated then returns the following
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        GameObject bullet = Instantiate(BulletPrefab, FirePoint.position, FirePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(FirePoint.up * bulletForce, ForceMode2D.Impulse);
	}
Comment

Unity Bullet script


public class BulletType : MonoBehaviour
{
    // Protected is less stric than private, more stric than public
    // Protected variables only accesible to this class and its child.
    protected string name;
    protected int bulletDamage;
    protected int bulletSpeed;

    protected virtual void Start()
    {

    }

    // Virtual means you can override this method in child classes
    protected virtual void Damage() { }

    public virtual void PlaySound() { }

    protected virtual void ShowEffect() { }
}

Comment

PREVIOUS NEXT
Code Example
Csharp :: string from byte array c# 
Csharp :: unity detect if animation is playing 
Csharp :: how to check file path is valid in c# 
Csharp :: void on collision enter 2d 
Csharp :: c# string newline 
Csharp :: vb.net open file with default program 
Csharp :: get all sundays between two dates c# 
Csharp :: get enum int by name 
Csharp :: C# get enum value by DescriptionAttribute 
Csharp :: get desktop path c# 
Csharp :: time.timescale 
Csharp :: unity vscode launch.json 
Csharp :: wpf update listview itemssource 
Csharp :: c# and 
Csharp :: c# override index operator 
Csharp :: unique id c# 
Csharp :: c sharp array to list 
Csharp :: get length of a string c# 
Csharp :: c# round number 
Csharp :: C# Console multi language 
Csharp :: ubuntu: how to open the terminal from c# 
Csharp :: c# writeline list 
Csharp :: c# streamwriter 
Csharp :: c# array of strings 
Csharp :: how to pass class type to method c# 
Csharp :: csproj include folder and files 
Csharp :: unity 2d player move 
Csharp :: c# get offset from timezone 
Csharp :: wpf richtextbox clear text 
Csharp :: how to redirect to extern page in .net core 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =