Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Picked up object goes through walls

Discussion in 'Physics' started by Sqwideeee, Aug 11, 2021.

  1. Sqwideeee

    Sqwideeee

    Joined:
    Feb 22, 2020
    Posts:
    2
    So i made a gravity gun using this tutorial :


    Everything works great but when the object is picked up and its flying in the air it will go through the wall. I don't know what to do about it so please somebody help me.

    here is how it looks like :
     

    Attached Files:

  2. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    I've watched the tutorial and the problem is that while you are carrying the object it is kinematic and that's why it goes through walls. Imo it's not the best way to implement such mechanic. I would never set the object to kinematic and instead of moving it with moveposition, just add velocity to it in a direction that goes from the object to holding position.
    rigidbody.velocity += (holdingPoint.transform.position - rigidbody.position) * Time.deltaTime;
    Something like that. And ofc that should be executed in FixedUpdate().
     
    robijust and diXime like this.
  3. Sqwideeee

    Sqwideeee

    Joined:
    Feb 22, 2020
    Posts:
    2
    so i did something but i'm to dumb for this i think. Now when i click E on the object it suddenly disapears leaving 2 errors in console:

    Invalid worldAABB. Object is too large or too far away from the origin.

    rigidbody.velocity assign attempt for 'box' is not valid. Input velocity is { Infinity, -16251559578057951137656064367625502720.000000, -141491742411456750488595576672227426304.000000 }.
    UnityEngine.Rigidbody:set_velocity(Vector3)

    here is how i modified the code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class GravityPickUp : MonoBehaviour
    5. {
    6.     [SerializeField] Camera cam;
    7.     [SerializeField] float maxGrabDistance = 10f, throwForce = 20f, lerpSpeed = 10f;
    8.     [SerializeField] Transform objectHolder;
    9.     Rigidbody grabbedRB;
    10.  
    11.     void FixedUpdate()
    12.     {
    13.         if(grabbedRB)
    14.         {
    15.             grabbedRB.MovePosition(grabbedRB.velocity += (objectHolder.transform.position - grabbedRB.position) * Time.deltaTime );
    16.             if(Input.GetMouseButtonDown(0))
    17.             {
    18.                 grabbedRB.isKinematic = false;
    19.                 grabbedRB.AddForce(cam.transform.forward * throwForce, ForceMode.VelocityChange);
    20.                 grabbedRB = null;
    21.             }
    22.         }
    23.         if(Input.GetKeyDown(KeyCode.E))
    24.         {
    25.             if(grabbedRB)
    26.             {
    27.                 grabbedRB.isKinematic = false;
    28.                 grabbedRB = null;
    29.             }
    30.             else
    31.             {
    32.                 RaycastHit hit;
    33.                 Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
    34.                 if(Physics.Raycast(ray, out hit, maxGrabDistance))
    35.                 {
    36.                     grabbedRB = hit.collider.gameObject.GetComponent<Rigidbody>();
    37.                     if(grabbedRB)
    38.                     {
    39.                         grabbedRB.isKinematic = true;
    40.                     }
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
    c# for me is like black magic im completly new to it. Can you tell me what have i done wrong?
     
  4. entirelydoomed

    entirelydoomed

    Joined:
    Mar 26, 2021
    Posts:
    67
    This line here is completely wrong.
    (velocity += something) is basically the same as (velocity = velocity + something). I believe it is like that in almost every programming language, it's called increment operator. So here in this line of code you are incrementing(increasing) rigidbody's velocity by (objectHolder.transform.position - grabbedRB.position) every physics frame. But then you pass this whole expression as an argument to MovePosition method. Idk how this could even compile because it doesn't make any sence at all, no wonder it's giving you an error.
    If you are dealing with kinematic rigidbody (which you are accoring to your code) you should replace this line with:
    grabbedRB.MovePosition( (objectHolder.transform.position - grabbedRB.position) * Time.deltaTime );
    But, as i already adviced you previously, you don't really need to switch rigidbody to kinematic and then switch it back. Just work with normal dynamic rigidbody so that it would properly interact with surroundings while you carry it. So for dynamic rigidbody replace this line with:
    rigidbody.velocity += (holdingPoint.transform.position - rigidbody.position) * Time.deltaTime;
    and remove all the switching to kinematic and back because you don't need it anymore.

    Also if you feel like C# is too hard for you, consider using visual scripting tool such as Bolt, this way you could program without writing any code.
     
  5. Oliver0769

    Oliver0769

    Joined:
    Apr 28, 2021
    Posts:
    11
    Even though there are some bugs, everything looks so realistic. I admire the people who developed this program.