I want to create a jump pad when the player enters a collider he should get a vertical boost
My Player is instantiated at runtime without a rigidbody and I have attached the following Script to a Cube with a Box Collider (is Trigger = active) BouncePad is also activ
I have no errors in the console but nothing happens…
I would appreciate if anyone has an idea how to achieve that!
using UnityEngine;
namespace TheKit
{
/// <summary>
/// Use this to apply damage or kill player with triggers
/// </summary>
public class Kit_PlayerTriggerZone : MonoBehaviour
{
public bool BouncePad;
[Tooltip("BouncePad?")]
/// <summary>
public float AmountOfForce = 20;
private void OnTriggerEnter(Collider other)
{
Kit_PlayerBehaviour pb = other.transform.GetComponent<Kit_PlayerBehaviour>();
if (pb)
{
//Check if its our
if (pb.photonView.IsMine)
{
if (BouncePad)
{
other.transform.TransformDirection(Vector3.up * AmountOfForce);
}
}
}
}
}
}
He meant on the player.
If the player has a rigidbody, you can just do other.GetComponent().AddForce(Vector3.up * AmountOfForce) to bounce the object upwards.
You dont necessarily need a Rigidbody tho. It depends on how you handle your player movement. If you maintain your own velocity vector you can add the force to that manually. If the game is mostly physics based anyways a rigidbody would be the correct approach tho.
One main problem with your current approach is that you seem to misunderstand TransformDirection. It does absolutely nothing in your current context. Transform.TransformDirection(Vector3 direction) transforms the given Vector3 from local space to world space.
It does nothing to the position of the transform, it returns a value. What you intend to do is apply a force. Which you can most easily achieve using a rigidbody on the force-receiving object, or by managing your own velocity vector in your player movement script.
My bad, i meant OP does not necessarily need to use rigidbody based movement and could instead apply his own “force” to his own velocity vector. Having something like that is a common approach on a lot of character controller designs, and controlling a player through actual physics can be tricky for most games. Which is why i wanted to mention it, but arguably worded it pretty horribly.
If you make the Rigidbody kinematic, you can use the CharacterController. The Rigidbody will no longer care about physics, but it will still cause OnTriggerEnter to be called when your character’s collider intersects the other collider.
If you want to use OnTriggerEnter, you are going to have to use a Rigidbody. Nothing else will generate a call to OnTriggerEnter.
hey thanks again for your feedback…
I have added a rigidbody to the player and set it to kinematic but nothing happens… when the player enters the collider
using UnityEngine;
namespace TheKit
{
/// <summary>
/// Use this to apply damage or kill player with triggers
/// </summary>
public class Kit_PlayerTriggerZone : MonoBehaviour
{
public bool BouncePad;
[Tooltip("BouncePad?")]
/// <summary>
public float AmountOfForce = 20;
private void OnTriggerEnter(Collider other)
{
Kit_PlayerBehaviour pb = other.transform.GetComponent<Kit_PlayerBehaviour>();
if (pb)
{
//Check if its our
if (pb.photonView.IsMine)
{
if (BouncePad)
{
other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce);
}
}
}
}
}
}
hi,
thanks a lot for your help! i really appreciate that…
I just inserted your line and yes I get the Trigger Entered in the console (check the image)
but i have used the trigger system already with other stuff (kill on enter for example) and it works just fine… the only thing that doesn´t work is the jump pad and I am struggling already for days…
I’ll just throw this in here: ForceMode.Force (the default) is for applying a force to a body. So it expects the force to be applied every physics cycle. For things like explosions or jumping, where we have one instance applying all the force at once, we would want to use ForceMode.Impulse. So even though 800 is a pretty large value, the force being applied this cycle may overall be too small to make a noticable difference.