Source Code for bullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour {
public float speed = 50.0f;
private Rigidbody2D rb;
private Vector2 screenBounds;
public GameObject explosion;
// Use this for initialization
void Start () {
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(speed, 0);
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update () {
if(transform.position.x > screenBounds.x * -2){
Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D other){
if(other.tag == "asteroid"){
GameObject e = Instantiate(explosion) as GameObject;
e.transform.position = transform.position;
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
}
Source Code for desktopMovement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class desktopMovement : MonoBehaviour {
public Transform player;
public float speed = 5.0f;
public GameObject bulletPrefab;
// Update is called once per frame
void Update(){
moveCharacter(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")));
if(Input.GetKeyDown("space")){
shootBullet();
}
}
void moveCharacter(Vector2 direction){
player.Translate(direction * speed * Time.deltaTime);
}
public void shootBullet(){
GameObject b = Instantiate(bulletPrefab) as GameObject;
b.transform.position = player.transform.position;
}
}
Resources