Skip to main content

Posts

Restricted Bullets

In this script, fire rate of bullet is controlled i.e, only 1 bullet fire at a time. public GameObject Bullet; //Drag and drop Bullet prefab inside Bullet in the inspector panel public Transform Position; //Drag and drop Shoot position inside position in the inspector panel float Attack_Timer = 0.3f; float Current_Attack_Timer; bool CanAttack; void Update( )   {    Attack( ) ;   } void Attack( )  {    Attack_Timer += Time.deltaTime;   if(Attack_Timer > Current_Attack_Timer)    {      CanAttack = true;    }  if(Input.GetKeyDown(KeyCode.Space)   {      if(CanAttack)        {           Instantiate(Bullet,Position.position,Quaterninon.identity);           CanAttack = false;           Attack_Timer = 0f;         }    }  }          
Recent posts

CountDown Timer

This script will start count down timer in your game scene. Create empty gameobject name it "TimerManager" attach "TimerManagerScript" to it. Now inside TimerManagerScript, Using UnityEngine.UI;        //Namespace required to include Text  public Text CountDownText;  //Drag and Drop UI Text inside CountDownText in inspector panel. float StartTime; float CurrentTime; void Start( ) {   StartTime = 20f;   CurrentTime = StartTime;  } void Update( )   {    CurrentTime -= 1*Time.deltaTime;    CountDownText.text = CurrentTime.ToString("0");  }

Background Scaling

This script will scale the background for different screen resolutions. void Start()     {         SpriteRenderer sr = GetComponent<SpriteRenderer>();         transform.localScale = new Vector3(1, 1, 1);       //reset scale         float Height = sr.sprite.bounds.size.y;         float Width = sr.sprite.bounds.size.x;         float WorldHeight = Camera.main.orthographicSize * 2f;         float WorldWidth = WorldHeight / Screen.height * Screen.width;         Vector3 tempscale = transform.localScale;         tempscale.x = WorldWidth / Width + 0.1f;         tempscale.y = WorldHeight / Height + 0.1f;         transform.localScale = tempscale;     }

Making Singleton

Singleton Script  Let us make a GameManager Script which will be singleton and will not destroy between scene loading or changing. First of all create empty Gameobject in hierarchy inside unity game engine, rename it to "GameManager". Now click on Add Component inside the inspector panel and create a new script namely "GameManagerScript". Open GameManagerScript in Visual Studio/ Monodevlop, Write the following code : using UnityEngine; public class GameManagerScript : MonoBehaviour {    public static GameManagerScript instance;    void Awake()   {          if(instance != null)          {           Destroy(gameobject);           }          else          {           instance = this;           DontDestroyOnLoad(gameobject);           } ...