Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with bounce on collision

Discussion in '2D' started by Tellek96, Sep 29, 2016.

  1. Tellek96

    Tellek96

    Joined:
    Sep 29, 2016
    Posts:
    4
    Hi there,
    I am learning how to stuffs work in unity and I actually need some help over here.
    I m writing a simple space shooter game and i want a player (the starship obviously) bounce from the edge of the screen when he reach it.
    I got 2 collidors, one on each side with script attached:
    Code (CSharp):
    1.     public float bounceForce = 5f;
    2.  
    3.  
    4.     void OnTriggerEnter2D(Collider2D col){
    5.  
    6.         name = col.gameObject.name;
    7.  
    8.         if (name == "player"){
    9.             Debug.Log ("u re in me");
    10.             GetComponent<Rigidbody2D> ().AddForce (new Vector2(-transform.position.x*bounceForce,0));
    11.             //col.attachedRigidbody.AddForce (transform.right * speed);
    12.             //GetComponent<Rigidbody2D> ().AddForce (transform.right * bounceForce);
    13.             //transform.Translate (Vector2.right * bounceForce);
    14.         }
    15.    
    16.     }
    I want add the counter force multplied by bunceForce in the direction from which the starship came.
    As you can see I tried may ways to achive that.
    Also I made it working once but since forced was added the spaceship kept moving right and wouldnt stop.
    Can you explain me how to make this bounce done?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    The reason why it kept moving right and wouldn't stop is either because you need to give your Rigidbody some drag, or you need to use ForceMode2D.Impulse as the second parameter in AddForce.

    Code (CSharp):
    1. GetComponent<Rigidbody2D> ().AddForce (new Vector2(-transform.position.x*bounceForce,0), ForceMode2D.Impulse);
     
  3. Tellek96

    Tellek96

    Joined:
    Sep 29, 2016
    Posts:
    4
    Well it doesnt work, actually the collider detects that i am hitting it but nothing more happens.
    I ve also done some research and tried this:
    Code (CSharp):
    1. col.gameObject.GetComponent<Rigidbody2D> ().AddForce (new Vector2(-transform.position.x*bounceForce,0), ForceMode2D.Impulse);
    And still nothing.
    On my player the "is kinematic" is checked, also same on my collideros + they got "is trigger" checked too.
    Without those options it even wont detect the collision.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    "isKinematic" means that the Rigidbody will not respond to forces, including collisions. You probably want to uncheck that.
     
  5. Tellek96

    Tellek96

    Joined:
    Sep 29, 2016
    Posts:
    4
    I ve uncheckd the "iskinematic" in both, the player and both side collidors so the spaceship is reciving force from them on hit but the force remaining utill the ship hits the opposite side collider which is weird and its not happening all the time, sometime spaceship bounces off the map lol.
    Also I have to got checked iskinematic on my spaceship coz when I shoot bullets the starship is recieving counter force somehow and its going in weird directions.
    I am getting really disappointed coz i see no clue in figuring this stuff out.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    It sounds like you have other issues with how you set up your physics objects.

    The "counter force" is maybe due to the bullets colliding with the ship when you spawn them. You can prevent your ship from colliding with the bullets by using Physics2D.IgnoreCollision when you spawn a bullet.

    You may need to give your ship's rigidbody a higher Drag value.
     
  7. Tellek96

    Tellek96

    Joined:
    Sep 29, 2016
    Posts:
    4
    Thanks, I ll try to figure this out
     
  8. Uleris

    Uleris

    Joined:
    Jun 16, 2016
    Posts:
    20
    This collider must be triger
     
  9. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Now I'm not sure this will help but I have an idea, you could calculate the distance to the edge of the screen if it lets say 0.1F from it bonce it the other way not sure how you simulate bouncing.

    Although you could setup a couple of edge colliders(left and right screen) and when it touch one of the it should bounce off still a material might help, I might be totally wrong what your trying to do with this. maybe a little image might clear it up more.
     
  10. Jared_Pitchford

    Jared_Pitchford

    Joined:
    Jul 7, 2018
    Posts:
    3
    set Collision detection to Continuous
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Please note the dates of threads. It's unlikely you're helping the person from 2016 here.