Currently I’m working on a game where when you run into an object you will die and a death screen will pop up. There is an option to continue for 100 coins which when you press it you will be re spawned back to where you died and should have invincibly for 2 seconds to where you don’t collide with anything for that certain amount of given time. The re spawning of the character works but the player collides with the object instead of being able to have invincibility for 2 seconds. I don’t know what in my code is causing this, help would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour
{
public GameManager theGM;
public Rigidbody theRB;
public float jumpForce;
public Transform modelHolder;
public LayerMask whatIsGround;
public bool onGround;
public Animator anim;
private Vector3 startPosition;
private Quaternion startRotation;
public float invincibleTime;
private float invincibleTimer;
// Start is called before the first frame update
void Start()
{
startPosition = transform.position;
startRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if(theGM.canMove)
{
onGround = Physics.OverlapSphere(modelHolder.position, 0.2f, whatIsGround).Length > 0;
if(onGround)
{
if (Input.GetMouseButtonDown(0))
{
//Make the player jump
theRB.velocity = new Vector3(0f, jumpForce, 0f);
}
}
}
//control invincibility
if(invincibleTimer > 0)
{
invincibleTimer -= Time.deltaTime
}
//control animations
anim.SetBool("walking", theGM.canMove);
anim.SetBool("onGround", onGround);
}
//checks for a collider with a trigger
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Hazards")
{
if (invincibleTimer <= 0)
{
Debug.Log("Hit Hazard");
theGM.HitHazard();
//theRB.isKinematic = false;
theRB.constraints = RigidbodyConstraints.None;
theRB.velocity = new Vector3(Random.Range(GameManager._worldSpeed / 2f, -GameManager._worldSpeed / 2f), 2.5f, -GameManager._worldSpeed / 2f);
}
}
if(other.tag == "Coin")
{
theGM.AddCoin();
Destroy(other.gameObject);
}
}
public void ResetPlayer()
{
theRB.constraints = RigidbodyConstraints.FreezeRotation;
transform.rotation = startRotation;
transform.position = startPosition;
invincibleTimer = invincibleTime;
}