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. Dismiss Notice

My 2d objects won't collide with each other

Discussion in '2D' started by dcruz26, Jul 13, 2016.

  1. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Which objects are you talking about? What is happening? How did you want the objects to work? One screenshot is not enough to give any sort of advice.
     
  3. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    um my columns and the round objects both have rigidbody2d,box collider and circle collider but they wont collide with each other they just pass through each other
     
  4. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    what part of the program can i show you so you can help me:)
     
  5. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  6. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  7. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  8. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  9. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  10. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    upload_2016-7-13_23-6-39.png the ball and the colums wont collide
     

    Attached Files:

  11. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    229
    You still missed the main point, we would like to see the script where you handle the movement of the 2D objects. :)
    My wild guess is that you are moving them with tranform.position, or tranform.translate and therefore you are placing them over eachother overriding physics.
     
    b_sefer_sa likes this.
  12. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
  13. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    public class CircleMovement : MonoBehaviour
    {

    Vector3 velocity = Vector3.zero;
    float bouncespeed = 10f;
    float forwardSpeed = 1f;

    Animator animator;
    public bool dead = false;
    float deathCooldown;
    public bool godMode = false;

    bool didjump = false;







    // Use this for initialization
    void Start()
    {

    animator = transform.GetComponentInChildren<Animator>();

    if (animator == null)
    {
    Debug.LogError("Didn't find animator!");
    }
    }

    // Do Graphic & Input updates here
    void Update() {


    if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
    {
    didjump = true;
    }

    }


    void FixedUpdate()

    {

    if (dead)
    return;


    GetComponent<Rigidbody2D>().AddForce(Vector2.right * forwardSpeed);

    if (didjump)
    NewMethod();

    if (GetComponent<Rigidbody2D>().velocity.y > 0)
    {
    transform.rotation = Quaternion.Euler(0, 0, 0);
    }
    else {
    float angle = Mathf.Lerp(0, -90, (-GetComponent<Rigidbody2D>().velocity.y / 3f));
    transform.rotation = Quaternion.Euler(0, 0, angle);
    }
    }

    private void NewMethod()
    {
    GetComponent<Rigidbody2D>().AddForce(Vector2.right * forwardSpeed);
    GetComponent<Rigidbody2D>().AddForce(Vector2.up * bouncespeed);
    NewMethod1();

    didjump = false;
    }

    private void NewMethod1()
    {
    animator.SetTrigger("dobounce");
    }

    OnCollisionEnter2D void (Collision2D collision)
    {
    if (godMode)
    return;

    animator.SetTrigger("Death");
    dead = true;
    deathCooldown = 0.5f;
    }
    }
     

    Attached Files:

  14. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    public class Camerafollow : MonoBehaviour
    {
    Transform player;

    float offsetX;

    // Use this for initialization
    void Start()
    {
    GameObject player_go = GameObject.FindGameObjectWithTag("Player");

    if (player_go == null)
    {
    Debug.LogError("Couldn't find an object with tag 'Player'!");
    return;

    }

    player = player_go.transform;

    offsetX = transform.position.x - player.position.x;
    }

    // Update is called once per frame
    void Update()
    {
    if (player != null)
    {
    Vector3 pos = transform.position;
    pos.x = player.position.x + offsetX;
    transform.position = pos;

    }
    }
    }
     
  15. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    public class column : MonoBehaviour {
    public Vector2 obstacleVelocity = new Vector2 (-7,0);

    // Use this for initialization
    void Start () {
    GetComponent<Rigidbody2D>().velocity = obstacleVelocity;
    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  16. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Well, you went from too little information to too much information lol. Thanks for posting more info, but try to be more concise. All we need to know is what you want to happen, what is currently happening, and how you're currently trying to do it. Next time please use Code Tags when posting snippets of code.

    Do you fully understand what setting an object to Kinematic does?

    https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

    "Forces, collisions or joints will not affect the rigidbody anymore."

    I have a feeling that is your issue. Try making your character Non-Kinematic.
     
  17. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    Thank you for the advice hahahaha im very new in coding i just started learning so HAHAHA im not good
     
  18. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    the background is kinamatic if i un-kinematic it the background falls
     
  19. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Not a problem, you're in the right place. Please let me know if that fixes your issue, or if you're still having trouble with your collisions.
     
  20. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    The background can be kinematic if it doesn't need to move using physics. The background likely doesn't need a rigidbody at all, just a collider. Only one of the objects colliding requires a Rigidbody component. In this case that would be your circle character.

    Also, it falls because the rigidbody gravity is set to a value above 0.
     
  21. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    even if i put the gravity to zero it still falls
     
  22. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    OHHH THANKSS!
     
  23. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    i changed the value to a negative value but it still becomes tilt
     
  24. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I think you can remove the rigidbody from your obstacles. They don't need to move with physics, right? They just need to stay still and block the character? If that sounds right, then all they need is a collider. Your circle guy needs a collider and a rigidbody to move using physics.
     
  25. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    OK thanks i get my problem and i have a last question if i have a column i want to put upside down but it is not kinematic how can i do that?
     
  26. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    how can i hang the column?
     
  27. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    When you say "hang" do you mean simply be upside down and on the top of the screen? Or do you want it to use physics and move when the character hits it?
     
  28. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    um i solved it already i have a new problem my object collides with the column but when it collides it should change scene this is my script for that
    public Vector2 jumplength = new Vector2(0f, 300f);
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
    {
    GetComponent<Rigidbody2D>().velocity = Vector2.zero;
    GetComponent<Rigidbody2D>().AddForce(jumplength);
    }

    }
    void Die()
    {
    SceneManager.LoadScene(2);
    }

    void OnCollisionEnter2D(Collider2D collision)
    {
    if (collision.gameObject.tag == "enemy")
    { Die(); }
    }
    }
    but it won't change scene
     
  29. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    229
    Please use CODE TAGS: http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    First of all, you dont need to get the rigidbody2d component every frame, its enough to get it once :)
    Code (CSharp):
    1.  
    2. public Vector2 jumplength = new Vector2(0f, 300f);
    3. private Rigidbody2D theRigidbody;
    4. // Use this for initialization
    5. void Start()
    6. {
    7. theRigidbody =GetComponent<Rigidbody2D>(); //Get the Rigidbody2D component
    8. }
    9.  
    10. // Update is called once per frame
    11. void Update()
    12. {
    13.   if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
    14.   {
    15.     theRigidbody.velocity = Vector2.zero; //Set the velocity of the rigibody2d to 0
    16.      theRigidbody.AddForce(jumplength);//Add some force
    17.   }
    18. }
    19. void Die(){
    20.         SceneManager.LoadScene(2);
    21. }
    22.  
    23. void OnCollisionEnter2D(Collider2D collision)
    24. {
    25.     if (collision.gameObject.tag == "enemy") {
    26.           Die();
    27.    }
    28. }
    29. }

    Secondly, your mistake is that the columns do not have their tags set to enemy.
    In order to do that you should:
    Reference: https://docs.unity3d.com/Manual/Tags.html
     
  30. dcruz26

    dcruz26

    Joined:
    Jul 13, 2016
    Posts:
    39
    i put the tag in enemy on the columns it still wont change scene :(
     
  31. INDUWAARA

    INDUWAARA

    Joined:
    Apr 13, 2020
    Posts:
    4
    hey freinds how do i make the rigidbody dynamic and also the player doesn't fall
     
  32. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Only 4 years old thread...

    And why not ask this in your own thread?
     
  33. INDUWAARA

    INDUWAARA

    Joined:
    Apr 13, 2020
    Posts:
    4
    that's right