Hi, I want my player (a capsule) to have a force added when colliding with a land mine (which is a cube). Specifically, I want the player to sort of 'fly off' in a random direction when close to the land mine (I checked is trigger for landmine and extended collision zone). Here's my script for collision detection, which is attached to the landmine:
As you can see, the addrelativeforce line isn't working for some reason, and I'm not sure why and how to fix it. Can someone please help? thanks.
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LandMine : MonoBehaviour
{
public GameObject explosionPref; // an explosion prefab, I put one in inspector slot
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "player"){ // detect player
GetComponent<Renderer>().enabled = false;
explosionPref.SetActive(true); //active explosion
other.gameObject.GetComponent<Rigidbody>().AddRelativeForce(Random.onUnitSphere * 3); // not working....????
Invoke("DestroyLandMine",2); // remove landmine after explosion
}
}
void DestroyLandMine(){
Destroy(gameObject);
}
}