Search Unity

Creating a Jump Pad in C#

Discussion in 'Scripting' started by dann96, Mar 22, 2015.

  1. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    For some reason, I can't get this to work.

    What I want is the platform to read if an object (player, ball, etc.) is OnTriggerEnter and, if so, apply transformdirection.

    public int force = 20;

    void OnTriggerEnter(Collider item)
    {
    item.transform.TransformDirection(Vector3.up * force);
    }

    (The script is applied to the platform, not the player or ball.)
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
  3. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    The problem is that the object isn't responding to anything. When the object is on the jump pad, it doesn't do anything but just pass through the pad.
     
  4. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Did you check that jump pad Collider script has Trigger checked on?
     
  5. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Make sure the player has a rigidbody. Its needed for detection.
     
  6. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    I sort of solved the problem, but now I have a new issue. The problem was that the trigger was not reading that the player has a collider. I am currently using the built-in Unity 5 fps controller which uses a character controller collider, but yet the trigger refuses this. How can I make the trigger read the controller as an actual collider?
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Make sure 1 of the things with a collider has a rigidbody attached
     
  8. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    Both the player and the jump pad have rigidbodies.
     
  9. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Is your Trigger collider set to "Is Trigger" ?
     
  10. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
  11. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
  12. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    The situation is that it works on other objects except the player, just for clarification.
     
  13. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    There is too little information to go on. Do a debug.Log like I said, and check if the name printed out is the name of your Player gameobject.
     
  14. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    FPSController
    UnityEngine.Debug:Log(Object)
    platform_bounce:OnTriggerEnter(Collider) (at Assets/scripts/environmental_features/platform_bounce.cs:11)

    (FPSController meaning the player object)

    Also, here is the error I get when the player collides with the platform:

    NullReferenceException: Object reference not set to an instance of an object
    platform_bounce.OnTriggerEnter (UnityEngine.Collider item) (at Assets/scripts/environmental_features/platform_bounce.cs:12)
     
  15. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Post line 12 of your platform_bounce script
     
  16. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    item.attachedRigidbody.velocity = transform.TransformDirection (Vector3.up * force);
     
  17. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    attachedRigidbody is obviously null. Is the rigidbody component below or above the gameobject which contains your player collider within the hierarchy? Or is it on the same gameObject?

    Also, like I said, transform.TransformDirection (Vector3.up * force);...will do absolutely nothing.
     
  18. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    The rigidbody is attached to the main player gameobject and is below the player collider.

    Also, the transformdirection statement does work as I placed a cube with a rigidbody and it caused the cube to bounce upward
     
  19. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
  20. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    the issue is that the script is not reading that the player has a collider attached, not a rigidbody. Technically, the player does have a collider, but it is a playercontroller component. How exactly do I get it to read the playercontroller component as a collider?
     
  21. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Wait what? If it doesn't have a collider component attached it doesn't have a collider. :/
     
  22. dann96

    dann96

    Joined:
    Dec 6, 2014
    Posts:
    113
    Technically, a charactercontroller component is qualified as a type of collider, yet the method does not recognize it.
     
  23. pappernabkin

    pappernabkin

    Joined:
    Mar 8, 2017
    Posts:
    1
    So i fixed and updated the script for you here it is bro

    public class JumpPad : MonoBehaviour {
    public GameObject PlayerCtrl;
    public float AmountOfForce = 20;

    void OnTriggerEnter(Collider PlayerCtrl){

    PlayerCtrl.transform.TransformDirection (Vector3.up * AmountOfForce);

    }
    }