Hei here is how I got it to work.
First here a basic move script:
public class Move : MonoBehaviour {
public Rigidbody2D rig;
public float speed = 0.2f;
public float maxSpeed = 5f;
void FixedUpdate ()
{
Vector2 vel = new Vector2 ( Input.GetAxis("Horizontal") /5f, Input.GetAxis("Vertical")/5f);
vel.Normalize ();
if (vel.sqrMagnitude > 0f && rig.velocity.sqrMagnitude < maxSpeed) {
rig.AddForce (vel * speed, ForceMode2D.Impulse);
} else {
rig.velocity = Vector2.zero;
}
}
}
And then the bouncing script:
public class CollideCtrl : MonoBehaviour
{
public float speed = 500f;
void OnCollisionEnter2D (Collision2D col) {
if(col.gameObject.CompareTag("Player")){
Debug.Log("Col");
Rigidbody2D rig = col.gameObject.GetComponent<Rigidbody2D>();
if(rig == null) { return;}
Vector2 velocity = rig.velocity;
rig.AddForce( -velocity * speed);
}
}
}
You now need to tweak those values. The effect works but is not perfect to my taste. I invite anyone to improve that answer with their suggestion or own answer coz this is a quick way but not a perfect one. That may give you some lead.
The player needs a Player tag, Rigidbody2D with no gravity and a 2D collision box. The box to collide with needs a BoxCollider2D and isTrigger as false. If you need it as trigger, then change the name and parameter of the collision method.