Search Unity

Creating a trigger to blast the player upwards

Discussion in 'Physics' started by cookywooky, Aug 30, 2020.

  1. cookywooky

    cookywooky

    Joined:
    Aug 21, 2020
    Posts:
    3
    Hi so I'm a definite newbie and I'm still figuring out a lot of the basics of C# and Unity. I've been working on a little 2D platformer project and I'm trying to code a kind of launchpad, which the player will walk over and will then shoot them into the air, as well as play the animation. The launcher is in the form of a water spout.

    There are a few problems:
    1) The player jump animation doesn't trigger when stepping on the launchpad. Is this something I should code into the actual player animation script?
    2) The launchpad works perfectly if you run up to it, but if you jump on it, it either doesn't trigger at all or works differently (like 3 small jumps, followed up a proper jump)
    3) the last thing is that I need to drag my character controller for my player to the right space in the inspector window for the launchpad each time I run the scene. I have it set as such before-hand, but it always gets set to "none" when I run it, and then I have to do it manually

    Any help/tips would be much appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spout : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     public float spoutThrust = 20f;
    9.     public CharacterController2D player;
    10.     private PlayerMoveScript playerMove;
    11.  
    12. void Start()
    13. {
    14.     player = GetComponent<CharacterController2D>();
    15.     playerMove = GetComponent<PlayerMoveScript>();
    16. }
    17.  
    18. private void OnTriggerEnter2D(Collider2D collider)
    19. {
    20.     Debug.Log("Triggered the spout!");
    21.     animator.SetBool("PlayerContact", true);
    22.     if (collider.tag == "Player")
    23.     {
    24.             player.m_Grounded = false;
    25.             player.m_Rigidbody2D.AddForce(new Vector2(0f, player.m_JumpForce * spoutThrust));
    26.             playerMove.jump = true;
    27.             playerMove.animator.SetBool("IsJumping", true);
    28.     }
    29. }
    30.  
    31. private void OnTriggerExit2D(Collider2D collider)
    32. {
    33.     Debug.Log("The spout stopped");
    34.     animator.SetBool("PlayerContact", false);
    35. }
    36.  
    37. } //end
     
    Last edited: Aug 30, 2020