- Home /
Question by
arepp · Jan 13, 2018 at 05:45 PM ·
rigidbody2djumping
Detect RigidBody2D Collision Only With Certain Velocity
I am trying to follow this tutorial: https://www.youtube.com/watch?v=fHN-26GEVhA&t=1671s . When the object collides nothing happens, the upward velocity is not added. I am trying to do this so that the object is only affected by another object when it is falling, similar to doodle jump mechanics.
Using this code for my object :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bounce : MonoBehaviour {
public float jumpForce = 10f;
// Jumping on top of boxes and through from beneath
void OnCollisionEnter2D (Collision2D collision) {
if (collision.relativeVelocity.y <= 0f) {
Debug.Log ("less than");
Rigidbody2D cube = collision.collider.GetComponent<Rigidbody2D> ();
if (cube != null) {
Debug.Log ("reached");
Vector2 velocity = cube.velocity;
velocity.y = jumpForce;
cube.velocity = velocity;
}
}
//Debug.Log ("hit");
}
}
Comment
Your answer
Follow this Question
Related Questions
2D walls affect my jump height 1 Answer
player jumps too high when clicking with two fingers. 2 Answers
Simple Rigidbody2D movement and jump 0 Answers
I am a begginer trying to make a simple 2d platformer. Need help with rotation and jump height. 1 Answer
Trouble detecting when Rigidbody2D character lands. (Not so simple) 1 Answer