I keep having issues with using the Colliders to kill or reset the scene. I have looked up a tin of posts and everyone keeps saying to make sure the player has a Rigidbody. It does. And both player and object have colliders. They do. But every time I enable “Is Trigger” on an object, my player goes right through it. If I turn off the Trigger, the player collides with the object but nothing happens. This has happened on 3 different games I have been trying to build, with and without tutorials. Everything else is working fine and I am getting no errors from Unity. Thanks for any help you can give.
Here the code Im using for an endless runner:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
Player Movement:
public class Obstacle : MonoBehaviour
{
PlayerMovement playerMovement;
// Start is called before the first frame update
void Start()
{
playerMovement =GameObject.FindObjectOfType<PlayerMovement>();
}
private void OnCollisionEnter (Collision collision)
{
{
if (collision.gameObject.name == "player")
//Kill the Player
playerMovement.Die();
}
}
}
public class PlayerMovement : MonoBehaviour
{
bool alive = true;
public float speed = 5;
public Rigidbody rb;
float horizontalInput;
public float horizontalMultiplier = 2;
private void FixedUpdate ()
{
if (!alive) return;
Vector3 forwardMove = transform.forward * speed * Time.fixedDeltaTime;
Vector3 horizontalMove = transform.right * horizontalInput * speed * Time.fixedDeltaTime * horizontalMultiplier;
rb.MovePosition(rb.position + forwardMove + horizontalMove);
}
private void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (transform.position.y< -5)
{
Die();
}
}
public void Die ()
{
alive = false;
//Restart the game
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Obstacles:
public class Obstacle : MonoBehaviour
{
PlayerMovement playerMovement;
void Start()
{
playerMovement =GameObject.FindObjectOfType<PlayerMovement>();
}
private void OnCollisionEnter (Collision collision)
{
{
if (collision.gameObject.name == "player")
//Kill the Player
playerMovement.Die();
}
}
}