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

Dragged object goes through wall

Discussion in 'Scripting' started by qqepijackxu, Sep 29, 2020.

  1. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    26
    Google hasn't answered my question so I'll try here. Everything works fine but dragged object goes through walls. I have tried many ways to solve this but none of them works. I tried using OnTriggerEnter on walls, Overlapshere on dragged object to detect collision but can't get any of them work properly. I shortened the code a little so I hope i didn't remove anything important. Here is the code:


    Code (CSharp):
    1.     void OnMouseDown()
    2.  
    3.     {
    4.  
    5.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    6.         mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    7.     }
    8.  
    9.     private Vector3 GetMouseAsWorldPoint()
    10.  
    11.     {
    12.         Vector3 mousePoint = Input.mousePosition;
    13.         mousePoint.z = mZCoord;
    14.         return Camera.main.ScreenToWorldPoint(mousePoint);
    15.     }
    16.  
    17.     void OnMouseDrag()
    18.  
    19.     {
    20.         if (canpickup)
    21.         {
    22.             rb.MovePosition(GetMouseAsWorldPoint() + mOffset);
    23.             rb.isKinematic = true;
    24.         }
    25.  
    26.     }
    27.     void OnMouseUp()
    28.     {
    29.  
    30.         rb.isKinematic = false;
    31.  
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         float minDist = 2;
    37.         float dist = Vector3.Distance(Player.transform.position, draggedobject.position);
    38.         if (dist < minDist)
    39.         {
    40.             canpickup = true;
    41.         }
    42.         else
    43.         {
    44.             canpickup = false;
    45.         }
    46.  
    47.     }
    Pretty simple but can't understand the physics element here which I think is the problem.

    Thanks for help!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Kinematic objects are not affected by forces. Unity uses forces to depenetrate colliding objects. See the problem?

    The solution is to not make your object kinematic when you pick it up.
     
  3. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    26
    With kinematic off it still goes through walls and object starts glitching when picked up
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You'll probably need to move the object with forces rather than MovePosition. Apply a force that pushes the object towards the desired position from its current position.
     
  5. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    26
    This is so strange. Add.force doesn't work either. I also tried it with this script:

    https://gist.github.com/YoungjaeKim/6045432

    But still same effect: goes through walls. It does collide with other objects and I can hit objects with objects though.
     
  6. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    26
    Problem solved. Instead of OnMouseDrag and OnMouseDown I used raycasting and input.getmousebutton. Main key is rigidbody.velocity. Everything is ok now. Thanks for helping!
     
  7. forodeforis

    forodeforis

    Joined:
    Feb 16, 2022
    Posts:
    1
    Hi qqepijackxu, is possible to see your code? I'm struggling with the same issue but can't find a solution. Thank you