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:
- The player jump animation doesn’t trigger when stepping on the launchpad. Is this something I should code into the actual player animation script?
- 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)
- 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