- Home /
Question by
D0minick · Aug 19, 2019 at 07:11 AM ·
2d-platformershootingoncollisionenterpick up
I am trying to change the bulletPrefab OnCollisionEnter2D but its not working now that I changed to fire at mouse position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Weapon1 : MonoBehaviour
{
public PauseMenuScript GameIsPaused;
//public Transform firePoint;
GameObject bulletPrefab;
public GameObject fastBullet;
public GameObject slowBullet;
public GameObject standardBullet;
public float duration = 10f;
public float bulletVelocity = 20f; //new code for mouse position firing
public float fastbulletVelocity = 60f;
public float slowbulletVelocity = 10f;
public int maxAmmo = 50;
public int currentAmmo;
public float reloadTime = 1.5f;
private bool isReloading = false;
private bool cooldown = false;
public GameObject ammo;
public Text currentAmmoText;
public Text maxAmmoText;
public bool ReadyFire = true;
void Start()
{
currentAmmo = maxAmmo;
}
// Update is called once per frame
void Update()
{
currentAmmoText.text = currentAmmo.ToString();
maxAmmoText.text = maxAmmo.ToString();
if (isReloading)
return;
Shoot();
Fastshoot();
Slowshot();
/*if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}*/
if (Input.GetKeyDown(KeyCode.R))
{
bulletPrefab = standardBullet;
Debug.Log("Bullet type set back to standard.");
}
if (currentAmmo > maxAmmo)
{
currentAmmo = maxAmmo;
}
if (currentAmmo > 0)
{
ReadyFire = true;
}
if (currentAmmo <= 0)
{
ReadyFire = false;
}
if (currentAmmo <= 0 && Input.GetButtonDown("Fire1") && Time.timeScale > 0)
{
SoundManager.PlaySound("NoAmmoSound");
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("FastBulletPickup"))
{
bulletPrefab = fastBullet;
Destroy(GameObject.FindGameObjectWithTag("FastBulletPickup"));
Debug.Log("Fast bullets activated.");
StartCoroutine (Fast(other));
//ammo.GetComponent<Image>().color = Color.green;
}
if (other.CompareTag("SlowBulletPickup"))
{
bulletPrefab = slowBullet;
Destroy(GameObject.FindGameObjectWithTag("SlowBulletPickup"));
Debug.Log("Slow bullets activated.");
StartCoroutine(Slow(other));
//ammo.GetComponent<Image>().color = Color.blue;
}
/*
else
{
Shoot();
}
*/
/*if (other.CompareTag("FastBulletPickup"))
{
bulletPrefab = fastBullet;
Destroy(GameObject.FindGameObjectWithTag("FastBulletPickup"));
}
if (other.CompareTag("SlowBulletPickup"))
{
bulletPrefab = slowBullet;
Destroy(GameObject.FindGameObjectWithTag("SlowBulletPickup"));
}*/
}
IEnumerator Fast(Collider2D player)
{
if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
{
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
//currentAmmo--;
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
GameObject bulletPrefab = (GameObject)Instantiate(fastBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * fastbulletVelocity; // new code for fire from mouse position
SoundManager.PlaySound("WeaponSound");
currentAmmo -= 1;
Invoke("ResetCooldown", 1.5f);
cooldown = true;
}
if (GameIsPaused == true)
{
CancelInvoke();
}
//GetComponent<SpriteRenderer>().enabled = false;
//GetComponent<BoxCollider2D>().enabled = false;
yield return new WaitForSeconds(duration);
//bulletPrefab = standardBullet;
//ammo.GetComponent<Image>().color = Color.magenta;
}
void ResetCooldown()
{
cooldown = false;
}
IEnumerator Slow(Collider2D player)
{
if (Input.GetButtonDown("Fire") && Time.timeScale > 0 && ReadyFire == true)
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
GameObject bulletPrefab = (GameObject)Instantiate(slowBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
slowBullet.GetComponent<Rigidbody2D>().velocity = direction * slowbulletVelocity; // new code for fire from mouse position
SoundManager.PlaySound("WeaponSound");
currentAmmo -= 1;
/*if (GameIsPaused == true)
{
CancelInvoke();
}*/
}
//GetComponent<SpriteRenderer>().enabled = false;
//GetComponent<BoxCollider2D>().enabled = false;
yield return new WaitForSeconds(duration);
bulletPrefab = standardBullet;
ammo.GetComponent<Image>().color = Color.magenta;
}
void Shoot()
{
if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
GameObject bulletPrefab =
(GameObject)Instantiate(
standardBullet,
transform.position +
(Vector3)(direction * 2f),
Quaternion.identity); //new code fire based off mouse position
bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity; // new code for fire from mouse position
SoundManager.PlaySound("WeaponSound");
currentAmmo -= 1;
Invoke("ResetCooldown", 1.5f);
cooldown = true;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
CancelInvoke();
Time.timeScale = 0f;
}
}
void Fastshoot() //used to test different bullet types
{
if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
{
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
//currentAmmo--;
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
GameObject bulletPrefab = (GameObject)Instantiate(fastBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
bulletPrefab.GetComponent<Rigidbody2D>().velocity = direction * fastbulletVelocity; // new code for fire from mouse position
SoundManager.PlaySound("WeaponSound");
currentAmmo -= 1;
Invoke("ResetCooldown", 1.5f);
cooldown = true;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
CancelInvoke();
Time.timeScale = 0f;
}
}
void Slowshot() //used to test different bullet types
{
if (Input.GetButtonDown("Fire1") && Time.timeScale > 0 && ReadyFire == true && cooldown == false)
{
Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)((worldMousePos - transform.position));
direction.Normalize();
//Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); //old code fixed fire position
GameObject bulletPrefab = (GameObject)Instantiate(slowBullet, transform.position + (Vector3)(direction * 2f), Quaternion.identity); //new code fire based off mouse position
slowBullet.GetComponent<Rigidbody2D>().velocity = direction * slowbulletVelocity; // new code for fire from mouse position
SoundManager.PlaySound("WeaponSound");
currentAmmo -= 1;
Invoke("ResetCooldown", 1.5f);
cooldown = true;
/*if (GameIsPaused == true)
{
CancelInvoke();
}*/
}
if (Input.GetKeyDown(KeyCode.Escape))
{
CancelInvoke();
Time.timeScale = 0f;
}
}
IEnumerator Reload ()
{
isReloading = true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
}
But the issue I am having is that even when I get the pickups from OnCollisionEnter2D I can no longer get the prefab to change, compared to when I used to have the character just shoot from a set firepoint, any help would be greatly appreciated.
Comment