1

I have this topdown game on which I want to create a knockback effect when my player collides with my object. I want to create a knock back script using OnTriggerEnter2D. I want the knock back script to look like this. How Would I do that? (sorry I'm a bit of a noob)

(http://noobtuts.com/content/unity/2d-pong-game/vector2_directions.png)

wasicool2
  • 193
  • 1
  • 3
  • 15

2 Answers2

2

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.

Everts
  • 10,408
  • 2
  • 34
  • 45
  • Thank you for replying, but whats the move script for, and what should I attach it to – wasicool2 Dec 21 '15 at 12:28
  • It goes on the player. It controls the basic movement. I would suggest to create a simple level to prototype until you get it good and then move to your main scene. – Everts Dec 21 '15 at 12:34
  • Thank you I have just tried it out, but there is no force pushing my player back. it's just like when a object has a box collider2d and a rigidbody2d colliding with another object that has a box collider2d nothings happening but the rigidbody's shaking. – wasicool2 Dec 21 '15 at 12:39
  • Is there enough force? Try making speed variable bigger. – Everts Dec 21 '15 at 13:26
  • I increased the fore to: 1500 at first and then: 10000. but it still did the same thing – wasicool2 Dec 21 '15 at 13:29
  • Is the mass small enough on the rigidbody? It may sound like a waste of time but it is common to start a feature (like your bouncing) on a totally empty scene. This allows to get the things working and later on you move it to the main project. You should simply set the scene with one sprite and the collision script and one sprite to act as the player. I got it to bounce on my project. It did not look perfect but was a starting. – Everts Dec 21 '15 at 14:18
0

You could try using physics materials for this, documentation is here:

http://docs.unity3d.com/Manual/class-PhysicMaterial.html

It seems like just what you're looking for!

Al Wyvern
  • 199
  • 1
  • 15