Why is AddRelativeForce not adding force to player?

Joined
Nov 25, 2020
Messages
5
Reaction score
0
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:

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);
     }
 }
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.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
When tinkering, I found *3 to be extremely low. Low enough that it was unnoticeable. But, 300 or 1000 worked just fine. Try cranking up the force?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top