Hi there! I’m trying to make trampoline/spring work in all 4 directions (it is, up, down, left and right) for my 2D platformer project, just like the ones you can see in clasic Sonic games. Up and down springs are working well, lunching my player with the appropriate velocity and right direction, but the sideways springs are lunching player with very low velocity wich I can’t change, no matter what varaibles I’m using.
The code I wrote (wich is inspired by my movement script I traced and changed a little bit from this tutorial: 2D Character Movement Tutorial in Unity! - YouTube) goes like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpringMechanics : MonoBehaviour
{
[Header("Physics Related Variables")]
public Rigidbody2D rb;
[Header("Trampoline Jump Variables")]
[SerializeField] private float springJumpForce;
void OnCollisionEnter2D(Collision2D col)
{
TrampolineJump();
}
private void TrampolineJump()
{
Vector3 eulerAngles = transform.rotation.eulerAngles;
switch (eulerAngles.z)
{
case 0:
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(Vector2.up * springJumpForce, ForceMode2D.Impulse);
break;
case 90:
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(Vector2.left * springJumpForce, ForceMode2D.Impulse);
break;
case 180:
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(Vector2.down * springJumpForce, ForceMode2D.Impulse);
break;
case 270:
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(Vector2.right * springJumpForce, ForceMode2D.Impulse);
break;
}
}

