Some questions about a jump pad (2D)

Hi so I’m a definite newbie and I’m still figuring out a lot of the basics of coding, 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. I was told that this is because I have player = GetComponent() placed in Start(), although I’m not sure how to code this without doing that.

Any help/tips would be much appreciated.

(copied from another board because it seems to appropriate here.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Spout : MonoBehaviour
{
    public Animator animator;
    public float spoutThrust = 20f;
    public CharacterController2D player;
    private PlayerMoveScript playerMove;
 
void Start()
{
    player = GetComponent<CharacterController2D>();
    playerMove = GetComponent<PlayerMoveScript>();
}
 
private void OnTriggerEnter2D(Collider2D collider)
{
    Debug.Log("Triggered the spout!");
    animator.SetBool("PlayerContact", true);
    if (collider.tag == "Player")
    {
            player.m_Grounded = false;
            player.m_Rigidbody2D.AddForce(new Vector2(0f, player.m_JumpForce * spoutThrust));
            playerMove.jump = true;
            playerMove.animator.SetBool("IsJumping", true);
    }
}
 
private void OnTriggerExit2D(Collider2D collider)
{
    Debug.Log("The spout stopped");
    animator.SetBool("PlayerContact", false);
}
 
} //end

@cookywooky

  1. Yes

  2. You are using AddForce, which is a sum applied to your existing velocity. If you have an existing downward velocity, the force you add on the opposing direction will first need to account for the negative force. A solution I can suggest is setting the velocity instead, preserving the velocity on the X axis. player.m_Rigidbody2D.velocity = new Vector2(player.m_Rigidbody2D.velocity.x, player.m_JumpForce * spoutThrust);

  3. in the start method you say this player = GetComponent<CharacterController2D>(); This means that you are getting the characterController2D from the exact gameobject this script is also attached to. If it is not, it will return none. So if you want to drag it in using the inspector, you will need to remove the code from your start method in order to keep the inspector settings. It’s important to understand when dragging things into the script using the inspector is a good idea. For instance you can not drag something from the scene into a prefab in your assets.