Search Unity

My sprite is ignoring collision even though it has a collider

Discussion in '2D' started by kancelarija12, Nov 11, 2019.

  1. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    Title. The collisions are not small they are very fricking huge but it still ignores it. It is controled by mouse here is the script if it helps(it might not be inlined properly since i am writing this on phone):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move : MonoBehaviour
    6. {
    7.      private Vector3 mousePosition;
    8.      public float moveSpeed = 0.01f;
    9.  
    10.      void update()
    11.       {
    12.           if(Input.GetMouseButton(1)) //right one
    13.           {
    14.                mousePosition = Input.mousePosition;
    15.                mousePosition = Camera.main.ScreenToWorldPoint(mouseposition);
    16.                transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    17.            }
    18.        }
    19. }
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    First question is: Is your player on the exact same Z-axis as your other colliders or vice-versa. Secondly, are you using a Kinematic rigidbody to move with? Thirdly, if non-kenimatic RB is involved, is your collider set to isTrigger = true (Box will be checked for IsTrigger), and Finally, it appears as though you are teleporting your player rather than smoothly moving coordinates. I recommend trying to move in FixedUpdate() rather than Update() with your approach and perhaps your colliders will have enough frames to check if your player is hitting them or not.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    You should explain what you mean by "collisions" and what you're expecting. Collision callbacks, collision response?

    One thing that you should be aware of is that you should never, ever modify a Transform when using 2D physics. If you add a Rigidbody2D then you are putting it in charge of the Transform pose so work with it, not against it. Use Rigidbody2D.MovePosition instead.

    Note that you're teleporting the above GO to a position, not "moving it through space" to that position so you'll just caused overlaps which the solver has to figure out. Anyway, best to describe the body types you're talking about here. Dynamic, Kinematic and hopefully not Static (if you're moving them).

    I wanted to call this out as it makes no difference as this is 2D physics. Sorry, just didn't want to add confusion. :)