how to make 2D trampoline easy in unity

 how to make 2D trampoline  easy in unity

To make a 2D Trampoline you need the following asset:

  • make 2D Trampoline in Unity 2D
    The script Trampoline.cs .
  • a trampoline sprites or group of sprites .
  • a animation controller for the trampoline .
  • Bellow the components of my Trampoline :

make 2D Trampoline in Unity 2D

2 . The footer :

make 2D Trampoline in Unity 2D

trampoline footer








3 .  The top :




gameObject : Trampoline





4 . you can change Velocity value in the script component with your choice.


2D trampoline in unity - SF Games Tutorial

The Script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Trampoline : MonoBehaviour
{
    
    public Animator anim;
    public GameObject bouncer;
    public Vector2 velocity;



    // Use this for initialization
    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnCollisionStay2D(Collision2D other)
    {
        //Debug.Log("the player is staying on the trampoline");
        anim.SetBool("isStepped", true);
        Jump();

    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Player")
        {
           // Debug.Log("the player entred.");
           
           Debug.Log("enter trampoline");


        }

    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            // Debug.Log("the player exited.");
            anim.SetBool("isStepped", false);
           

        }
    }
    void Jump()
    {
        bouncer.GetComponent<Rigidbody2D>().velocity = velocity;
    }
}

The Trampoline Animation :

create two animations , one for idle and one for the animation jump.
Set a Bool named " isStepped " .
make your trampoline animation like you want.


in the jump animation add a new event ,deplace it in the position when the play start jumin on the trampoline 




Select the function