Hi there, I hope someone can help me a bit.. 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! Code (CSharp): 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<Rigidbody>().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. https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html 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.
To be clear, you mean if he doesn't want to rely on OnTriggerEnter, I take it? You do need a Rigidbody if you want to use a trigger.
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.
hey guys, thanks for your input.. actually I can´t use the rigidbody on my player because I am using the Character Controller on the Player... (I tried it though, with no succesful result) any idea how to achieve it without the rigidbody?
it doesn´t help.. as I said the Player is controlled by the CharacterController .. that´s why it doesn´t work with the Rigidbody.. I would need a solution without the rigidbody at all..
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 any idea why?
Code (CSharp): 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); } } } } } }
Let's put this between Line 17 and Line 18, run it again, and tell me if you get any output on the Console: print("Trigger entered");
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) https://ibb.co/HBLBMP1 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..
Well, at least we know the trigger is entered. What does this say if you put it after if (BouncePad)? print(AmountOfForce); So it reads like this: Code (CSharp): if (BouncePad) { print(AmountOfForce); other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce); }
Let's see if we can at least make it do something, then. Replace this: other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce); with this: other.transform.position = other.transform.position + Vector3.up; Does that make the other object move upward?
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. Code (CSharp): other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce, ForceMode.Impulse);
So the code is being executed. The problem seems to be that some other source edits the player position and resets it to the ground.
Yup. His code is running fine. To get it working the way he wants, I'd suggest putting it into a scene with as few other things in it as possible, so it can be tweaked and adjusted in isolation. If something else is taking control of the player position, he needs to get that out of the way. Also, when he says, " just a second or less.. than the player hits the ground again," it's possible that this is just physics yanking it down as it should. We'd have to see a video to know. xkingjohnx, can you do that, show us a video of what you're seeing?
Hi, I just recorded what happens with: other.transform.position = other.transform.position + Vector3.up; Here is the video: https://we.tl/t-QSJ3aL0Sbu
Looks like your character controller is yanking it back down. You'll need to add some kind jumping code to that controller, and replace other.transform.position = other.transform.position + Vector3.up; with a call to that code.