Creating a bouncy surface, where player is propelled with the same force as it hits
Hi,
I'm trying to create bouncy surfaces, where the player will be propelled to the same height from which it fell on collision. The same way the blue gel works in Portal 2, if I remember rightly!
(so, if the player jumps from a 3m high platform onto a bounce surface, they will be bounced back up to that height, and if they jump onto the same surface from 10m, they'll be launched 10m back up into the air, and so on)
The Bounce physics material does exactly this, but the material has to be applied to the falling object it seems, which makes it bounce off of everything. I don't want the player character to be bouncy, just bounce when hitting particular surfaces. Is there a way to attach the bounce material to the floor, rather than the object, and get the same behaviour? Or will it need a script?
Thanks in advance!
Answer by Osakajin · Jan 07, 2017 at 08:01 PM
You should be able to apply the physic material to whatever you want. In my experience, it helps to have a material with a Bounce value for everything, though, i.e. your player should have a bouncy physic material with a low bounce setting (unless your player is made of flubber) and the object should have its own material with a high bounce setting.
When I tried this, it didn't give enough bounce. For that project, though, my player objects were moving fast enough that I had a bullet-through-paper problem and my player could fly through the walls of the arena. My solution was to apply a physics force to the player rigid body at a certain distance from the wall's origin (it was a big sphere, which helped the math).
using UnityEngine;
using System.Collections;
public class Boundary : MonoBehaviour {
/// <summary>
/// This script causes a soft bounce effect on approaching the wall. This prevents the bullet-through-paper issue that we were having while relying strictly on the collider of the sphere to contain player movements.
/// The distance at which the effect is triggered is significantly smaller than the sphere's actual radius; this was due to smaller, faster ships being able to gain enough velocity to push all the way through the
/// sphere's mesh before the physics engine added enough backwards force to push them back inside.
/// </summary>
private Rigidbody rb;
void Awake () {
this.rb = GetComponent<Rigidbody>();
}
void Update () {
Vector3 directionToOrigin = this.transform.position;
if (directionToOrigin.sqrMagnitude > 249980) {
this.rb.AddRelativeForce(this.transform.InverseTransformDirection(-this.transform.position)*10);
//the scalar at the end of the above line will influence both how slowly a ship can be going and still touch the sphere collider, as well as how much of a push towards the middle they will get.
}
}
}
The effect was the player ship would suddenly slow down, barely kiss the wall, then get propelled backwards at about the same speed it was moving forwards. It worked for glancing blows, direct strikes, backwards... It might be overkill if your player isn't moving 50 m/s, but it makes for a very nice bounce.
And full disclosure, this project was over a year ago, it was my first non-tutorial project, and I've been on hiatus since shortly after writing this solution. There may be tricks to physic materials I don't know.
Your answer
Follow this Question
Related Questions
How do I get a ball to bounce inside of a circle? 1 Answer
create a bouncy surface using c# unity2D 0 Answers
Sumo Physics Bounce Question 0 Answers
Ball won't stop bouncing 1 Answer
Realistic bounce in a billiard game 0 Answers