I am currently trying to implement jump pads into my game. I have a trigger that detects when the player is on the jump pad and when the player is pressing space. If both of those are true, it uses AddForce to boost the player upwards. However, when I jump on the pad, it doesn’t boost me, even though my Debug.Logs are triggering.
The dots represent irrelevant code.
Here are the lines of code:
...
private Rigidbody playerBody;
public int boostHeight;
...
void Start () {
...
player = GameObect.FindGameObjectWithTag("Player");
...
playerBody = player.GetComponent<Rigidbody>();
}
...
if (state == State.boosting) {
Debug.Log("Boosting")
playerBody.AddForce(Vector3.up * boostHeigh, ForceMode.VelocityChange);
}
The “state” is an enum I set the value of in OnTriggerEnter, and it works just fine. The boostHeight is set in the editor to 200, and the if (state == State.boosting) is getting triggered, but for some reason the AddForce function just isn’t working, and, being a complete NOOB, I have no idea why.
Please help me,
-N251
P.S. Yes, the player has a Rigidbody. However, I am using the default FirstPersonController, and I have heard that using it might cause some problems.
I am assuming that the Debug.Log is being called, and the playerBody is not equal to null (this would throw an exception in the console). I’m also assuming that “boostHeigh” is a typo only in your post, and not in your actual code (as this would not compile!). Try writing this code in place of your AddForce:
playerBody.velocity = Vector3.up* boostHeight;
This won’t be the exact effect you’re going for, but it will let you know that you’re able to properly communicate with the rigidbody. Make sure that boostHeight is set to a reasonably high value to start as well, to make sure you’re not just adding an extremely tiny force (although ForceMode.VelocityChange adds a force independent of the rigidbody’s mass).
To me it looks like it isn’t working because you have the code within the “Start” method. On your booster object (the item that you stand on) I would add a trigger to it, then run the code in the trigger like this (untested):
public class Booster : MonoBehaviour {
public float boostHeight = 0;
public void OnTriggerEnter(Collider other) {
Rigidbody playerBody = other.GetComponent<Rigidbody>();
if (other.gameObject.tag == "Player" && playerBody) {
var state = other.gameObject<Player>().state;
if (state == State.boosting) {
playerBody.AddForce(Vector3.up * boostHeight, ForceMode.VelocityChange);
state = State.notBoosting;
}
}
}
}
I’m really sorry, I forgot to format and include things, and other stuff. Let me just post the entire document here. Of course, you will have to have Visual Studio to open it, or some other program, but I’m assuming you already have that.
This script is attached to a trigger that is above a prism that is on the ground.
You’re going to have to reconcile your script with your character controller, as the default first person controller uses CharacterController.Move to explicitly manipulate the transform of the object.
Hey, I realize this is an old post, but for anyone else here with a similar issue, like me: one thing that helped me was changing the Mass of my character. Setting it back to the default = 1 seemed to get it working, but more “realistic” values (representing kg) seemed to give “unresponsive” behavior, even though the code was working. Hope this helps someone!