I am trying to have my player game object have its Collider2D disabled for a short period of time right after spawning and then re-enabled there by giving the player a few seconds to re position to avoid chain spawn deaths. The code I have so far is below, I am not sure if my IEnumerator working because I need to provide an argument to StartCoroutine, right now its giving me an error.The idea is player spawns, collider2d is set to inactive, 3 seconds pass, and the collider is set back to active and stays that way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour {
public Boundary boundary;
public float invTime = 3f;
//Navigation Variables
public float speed;
public float strafespeed;
//Weapon Variables
public Transform shotSpawn1;
public GameObject shot;
public float fireRate;
private float nextFire;
private AudioSource audioSource;
private Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
}
private void Awake()
{
StartCoroutine(Invincibility());
}
IEnumerator Invincibility(Collider2D collider)
{
GetComponent<Collider2D>().enabled = false;
yield return new WaitForSeconds(invTime);
GetComponent<Collider2D>().enabled = true;
}
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn1.position, shotSpawn1.rotation);
audioSource.Play();
}
}
private void FixedUpdate()
{
var mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
rb.AddForce(Vector3.up * speed * v);
rb.AddForce(Vector3.right * strafespeed * h);
rb.position = new Vector3
(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(rb.position.y, boundary.yMin, boundary.yMax),
0.0f
);
}
}