I have spell cooldown variable :
private float nextFire = 0;
public bool IsOffCooldown()
{
return Time.time > nextFire;
}
public void Cast(Transform spellSpawnerTransform)
{
nextFire = Time.time + FireRate;
Instantiate(this, spellSpawnerTransform.position, Quaternion.identity);
}
Once I close the app the variable nextFire keeps it's previous value i.e if the last time the game went on for let's say 30 secs the nextFire variable will have 30f as starting value. Why ? How to fix this ?
UPDATE Full code
public class Spell : MonoBehaviour
{
public float FireRate;
public float Damage;
public float Speed;
public GameObject SpellExplosion;
public float nextFire = 0;
public bool IsOffCooldown()
{
return Time.time > nextFire;
}
private void Update()
{
}
public void Cast(Transform spellSpawnerTransform)
{
nextFire = Time.time + FireRate;
Instantiate(this, spellSpawnerTransform.position, Quaternion.identity);
}
}
SpellMover Class :
public class SpellMover : MonoBehaviour
{
private Rigidbody2D spellRigidBody;
private Vector3 sp;
private Vector2 dir;
private Spell spell;
private void Start()
{
spell = GetComponent<Spell>();
spellRigidBody = GetComponent<Rigidbody2D>();
sp = Camera.main.WorldToScreenPoint(transform.position);
dir = (Input.mousePosition - sp).normalized;
RotateTowardsMouse();
}
private void Update()
{
spellRigidBody.AddForce(dir * spell.Speed);
}
private void RotateTowardsMouse()
{
Transform target = spellRigidBody.transform;
Vector3 mouse_pos = Input.mousePosition;
Vector3 object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
float angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
spellRigidBody.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player" || other.name == "SpellSpawner" || other.name == "Boundary")
{
return;
}
Instantiate(spell.SpellExplosion, transform.position + transform.right, transform.rotation);
}
}
Player Movement Class :
public class PlayerMovement : MonoBehaviour
{
public float Speed;
public GameObject SpellToUse;
private Spell CurrentSpell;
private bool IsAttacking = false;
private Rigidbody2D playerRigidBody;
private Animator playerAnimator;
private SpriteRenderer playerRenderer;
private GameObject skillSpawner;
private void Start()
{
CurrentSpell = SpellToUse.GetComponent<Spell>();
playerRigidBody = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
playerRenderer = GetComponent<SpriteRenderer>();
skillSpawner = transform.FindChild("SpellSpawner").gameObject;
}
private void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
playerRigidBody.velocity = new Vector2(horizontal * Speed, vertical * Speed);
playerAnimator.SetFloat("Speed", horizontal);
if (Input.GetKey(KeyCode.Space) && CurrentSpell.IsOffCooldown())
{
CurrentSpell.Cast(skillSpawner.transform);
IsAttacking = true;
}
if (Input.GetKeyUp(KeyCode.Space))
{
IsAttacking = false;
}
playerAnimator.SetBool("IsAttacking", IsAttacking);
UpdateSpellSpawnerLocation(horizontal, vertical);
}
//separate script maybe ?
private void UpdateSpellSpawnerLocation(float inputHorizontal, float inputVertical)
{
if (inputHorizontal < 0)
{
//left
skillSpawner.transform.position = new Vector2(playerRigidBody.transform.position.x - playerRenderer.bounds.size.x / 3, playerRigidBody.transform.position.y);
}
else if (inputHorizontal > 0)
{
//right
skillSpawner.transform.position = new Vector2(playerRigidBody.transform.position.x + playerRenderer.bounds.size.x / 3, playerRigidBody.transform.position.y);
}
else if (inputVertical > 0)
{
//up
skillSpawner.transform.position = new Vector2(playerRigidBody.transform.position.x, playerRigidBody.transform.position.y + playerRenderer.bounds.size.y / 3);
}
else if (inputVertical < 0)
{
//down
skillSpawner.transform.position = new Vector2(playerRigidBody.transform.position.x, playerRigidBody.transform.position.y - playerRenderer.bounds.size.y / 2);
}
}
}