Why is AddRelativeForce not adding force to player?
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:
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);
}
}
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.
Your answer
Follow this Question
Related Questions
Character controller rocket jumping (adding force, while still maintaining control of the character) 0 Answers
making object move forward 0 Answers
How do I get objects in front of the player to be blasted away? 1 Answer
How to make a object move forward when is rotated. 0 Answers
How do I make a ball have force where it bounces and adds force in the direction it is going at? 0 Answers