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

Bug Tile Collision's not working in Unity2D. Any way to fix this?

Discussion in 'Scripting' started by iamv3nxm, Mar 6, 2023.

  1. iamv3nxm

    iamv3nxm

    Joined:
    Feb 10, 2023
    Posts:
    5
    For some reason, I cannot collide with objects. I'm working on a TOP DOWN RPG game and at first, I could collide, but when I added my animations, I couldn't.

    I looked up a bunch of ways to fix the issue, but nothing worked. I changed the order of the code but nothing worked. I thought by doing that it'd allow collisions. I even put a debug line so it could give me the results in the terminal, but nothing.

    Here's the code. Help will be much appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     private Rigidbody2D myRigidbody;
    9.     private Vector3 change;
    10.     private Animator animator;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start() {
    14.         animator = GetComponent<Animator>();
    15.         myRigidbody = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update() {
    20.         change = Vector3.zero;
    21.         change.x = Input.GetAxisRaw("Horizontal");
    22.         change.y = Input.GetAxisRaw("Vertical");
    23.         UpdateAnimationAndMove();
    24.     }
    25.  
    26.     void UpdateAnimationAndMove() {
    27.         if(change != Vector3.zero) {
    28.         {
    29.             MoveCharacter();
    30.         }
    31.             animator.SetFloat("MoveX", change.x);
    32.             animator.SetFloat("MoveY", change.y);
    33.             animator.SetBool("moving", true);
    34.         } else {
    35.             animator.SetBool("moving", false);
    36.         }
    37.     }
    38.  
    39.     void MoveCharacter() {
    40.         myRigidbody.MovePosition(
    41.             transform.position + change * speed * Time.deltaTime
    42.         );
    43.     }
    44. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,130
    This clue makes me think perhaps your animation is manipulating colliders, not just the graphics.

    AFAIK animating a collider causes the old one to be destroyed and a new one to be created at the new position, effectively a blind teleport from the Physics system's point of view. (Perhaps @MelvMay can agree or disagree here?)

    Ideally don't animate colliders that you rely on for receiving callbacks.

    If you absolutely must have animations actively "drive" colliders in a reactive way (eg, I stand near a wall and raise my arm into the wall to scratch my nose and my elbow actually pushes me away from the wall), you would need to implement some kind of IK system.

    This is generally never done in simple 2D RPG games.
     
  3. iamv3nxm

    iamv3nxm

    Joined:
    Feb 10, 2023
    Posts:
    5
    The thing is, I didn't intend on animating colliders. I just put the code in and BAM! The collision stopped working.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,130
    The thing is, you didn't yet confirm to me that you ARE animating colliders.

    Note my first sentence contains 'perhaps,' which is a signal to you to investigate and confirm or deny.

    (Of course you're welcome to disregard my post completely as well, in which case I have nothing else to suggest at this time, except for you to do some more debugging to find out what is happening.)
     
  5. iamv3nxm

    iamv3nxm

    Joined:
    Feb 10, 2023
    Posts:
    5
    I apologize for the way my last reply sounded. I have been up since 1 in the morning and had 5 hours of sleep. I figured it out, thank you for at least making an attempt to help me. I did debug a little more and I finally got it. I just needed to configure a few settings.
     
    Kurt-Dekker likes this.