Day #011

#DAY011

Shooting a limited number of bullets.

Main points covered

#public Rigidbody

#public Transform

#instantiate

 

C# script (shooter)

using UnityEngine;
using System.Collections;

public class Shooting : MonoBehaviour
{
public Rigidbody bulletPrefab;
public Transform firePosition;
public float bulletSpeed;
public int bulletss = 50;

void Update (){
Shoot();
}

void Shoot (){
if (Input.GetMouseButtonDown(0) && bulletss > 0)
{
Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, firePosition.rotation) as Rigidbody;
bulletInstance.AddForce(firePosition.forward * bulletSpeed);
bulletss–;
}
}
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *