- Home /
2D Horizontal trampoline/spirng issues
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: https://youtu.be/TTKPmPvekUY) 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;
}
}
Answer by Nistroy · Jun 28, 2021 at 12:45 PM
Hi @JanekMaster, you can use a 2D Collider box and define the bouciness from the material.
This doesn't seem to work as I intended. Bounce is different every time, depending where I touched the spring. Also if I touch, for example, top of the side-faced spring hitbox, I'm lunched upwards. But thanks for trying to help!
Your answer
Follow this Question
Related Questions
When I jump i teleport up and then glide down 0 Answers
how to limit horizontal input when jumping 1 Answer
Trouble with spring platform 0 Answers
rigidbody.AddForce jump, different heights each time 1 Answer
how to jump at fixed height but faster 0 Answers