Search Unity

Trying to make an object bounce off a wall

Discussion in '2D' started by BenX41, Nov 23, 2016.

  1. BenX41

    BenX41

    Joined:
    Oct 27, 2016
    Posts:
    10
    Hi new programmer here, made a script attached to a sphere that should make it bounce back upon colliding with a certain tag.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PushBack : MonoBehaviour {
    5.  
    6.     public float speed = 10;
    7.     public Rigidbody2D rb;
    8.     Vector3 pushRight;
    9.     Vector3 pushLeft;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         rb = GetComponent<Rigidbody2D>();
    14.         pushRight = new Vector3 (5f, 0f, 0f);
    15.         pushLeft = new Vector3 (-5f, 0f, 0f);
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     }
    22.        
    23.         void OnCollisionEnter2D(Collision2D other) {
    24.         if (other.gameObject.tag == "Right Wall") {
    25.             Debug.Log ("pushRight");
    26.             rb.AddForce (pushRight * speed);
    27.             }
    28.         if (other.gameObject.tag == "Left Wall") {
    29.             Debug.Log ("pushLeft");
    30.             rb.AddForce (pushLeft * speed);
    31.         }
    32.     }
    33. }
    34.  
    Not sure why but even though it shows as called in the console the ball doesn't bounce back.
    Any input would be appreciated.
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Use ForceMode2D.Impulse when applying an instantaneous force:

    rb.AddForce(pushRight * speed, ForceMode.Impulse);

    Rigidbody2D.AddForce() uses a Vector2 for its force value. Vector3's are implicitly converted to Vector2's by dropping the z-value, but in the future you may be able to save some typing when making new force vectors in-script.
     
    NIks_Cabs548 likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Shouldn't bouncing off the right wall be pushing left and vice versa?

    Here's another way to organize that logic:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PushBack : MonoBehaviour {
    5.  
    6.     public float pushForce = 10;
    7.     public Rigidbody2D rb;
    8.  
    9.     // Use this for initialization
    10.     void Start() {
    11.         rb = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update() {
    16.  
    17.     }
    18.  
    19.     void OnCollisionEnter2D(Collision2D other) {
    20.         if(other.gameObject.tag == "Right Wall") {
    21.             Debug.Log("pushLeft");
    22.             rb.AddForce(Vector2.left * pushForce, ForceMode2D.Impulse);
    23.         }
    24.         else if(other.gameObject.tag == "Left Wall") {
    25.             Debug.Log("pushRight");
    26.             rb.AddForce(Vector2.right * pushForce, ForceMode2D.Impulse);
    27.         }
    28.     }
    29. }
     
    Neo_44 and BenX41 like this.
  4. BenX41

    BenX41

    Joined:
    Oct 27, 2016
    Posts:
    10
    Thanks for the help that fixed my problem! ^^
     
    LiterallyJeff likes this.
  5. oguzkagan

    oguzkagan

    Joined:
    Feb 23, 2019
    Posts:
    2
    i found the solution in this video:
     
  6. anziel

    anziel

    Joined:
    May 8, 2021
    Posts:
    1
    kanka instagramın veya discordun var mı sana bi soru sorucaktım
    mustafagensi#8181 bu benim ki görüp yazarsan çok sevinirim