Search Unity

how to create a jump pad?!?

Discussion in 'Scripting' started by xkingjohnx, Oct 19, 2020.

  1. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    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):
    1. using UnityEngine;
    2.  
    3. namespace TheKit
    4. {
    5.     /// <summary>
    6.     /// Use this to apply damage or kill player with triggers
    7.     /// </summary>
    8.     public class Kit_PlayerTriggerZone : MonoBehaviour
    9.     {
    10.         public bool BouncePad;
    11.         [Tooltip("BouncePad?")]
    12.         /// <summary>
    13.      
    14.         public float AmountOfForce = 20;
    15.  
    16.         private void OnTriggerEnter(Collider other)
    17.         {
    18.             Kit_PlayerBehaviour pb = other.transform.GetComponent<Kit_PlayerBehaviour>();
    19.             if (pb)
    20.             {
    21.                 //Check if its our
    22.                 if (pb.photonView.IsMine)
    23.                 {
    24.                     if (BouncePad)
    25.                     {
    26.                         other.transform.TransformDirection(Vector3.up * AmountOfForce);
    27.  
    28.                     }
    29.            
    30.                
    31.                    }
    32.              }
    33.     }
    34. }
    35. }
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    You will need a
    Rigidbody
    on the object with the
    Collider
    set to
    isTrigger == true
    .
     
  3. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    thanks for your reply.. you mean on the jump pad itself?
    I tried that too, it doesnt work taht way..
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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.
     
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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.
     
    Yoreki likes this.
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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.
     
    Stevens-R-Miller likes this.
  7. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    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?
     
  8. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Use the Rigidbody and set
    isKinematic
    to
    true
    .
     
  9. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    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..
     
  10. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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.
     
    Yoreki likes this.
  11. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    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?
     
  12. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Can you post your current code?
     
  13. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace TheKit
    4. {
    5.     /// <summary>
    6.     /// Use this to apply damage or kill player with triggers
    7.     /// </summary>
    8.     public class Kit_PlayerTriggerZone : MonoBehaviour
    9.     {
    10.         public bool BouncePad;
    11.         [Tooltip("BouncePad?")]
    12.         /// <summary>
    13.      
    14.         public float AmountOfForce = 20;
    15.  
    16.         private void OnTriggerEnter(Collider other)
    17.         {
    18.             Kit_PlayerBehaviour pb = other.transform.GetComponent<Kit_PlayerBehaviour>();
    19.             if (pb)
    20.             {
    21.                 //Check if its our
    22.                 if (pb.photonView.IsMine)
    23.                 {
    24.                     if (BouncePad)
    25.                     {
    26.                        other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce);
    27.  
    28.                     }
    29.            
    30.                
    31.                    }
    32.              }
    33.     }
    34. }
    35. }
     
  14. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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");
     
  15. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    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..


     
  16. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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):
    1.     if (BouncePad)
    2.     {
    3.         print(AmountOfForce);
    4.         other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce);
    5.     }
     
    Yoreki likes this.
  17. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
  18. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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?
     
  19. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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):
    1. other.GetComponent<Rigidbody>().AddForce(Vector3.up * AmountOfForce, ForceMode.Impulse);
     
  20. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    yes, but just a second or less.. than the player hits the ground again
     
  21. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    I just tried that too, but it doesn´t do anything..
     
  22. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    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.
     
  23. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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?
     
  24. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    Hi,
    I just recorded what happens with: other.transform.position = other.transform.position + Vector3.up;

    Here is the video: https://we.tl/t-QSJ3aL0Sbu
     
  25. xkingjohnx

    xkingjohnx

    Joined:
    Feb 22, 2016
    Posts:
    67
    what I want to achieve is something like this:
     
  26. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    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.