How To Pick & Drop Physics Objects like Karlson | Unity

Поделиться
HTML-код
  • Опубликовано: 25 янв 2025

Комментарии • 104

  • @Boxply
    @Boxply  Год назад +5

    If you have any issues whatsoever (or just want to leave a commendation), feel free to let me know here in the comment section.

    • @strompy2007
      @strompy2007 Год назад +2

      In your script, you have set the line renderer position to "this.transform.position". But it will be better and it will have no more bug if you set it to "applicationPoint.position", line 115 in the DrawRope void.
      I'm working on a game like karlson, and so I used your script, I will named you in my last video if you see any trouble

    • @Boxply
      @Boxply  Год назад +1

      Thanks! And if that works better than go all for it! Good luck on your game

    • @micahlmao
      @micahlmao Год назад

      Can you make an updated version for unity 5?

    • @dontCallMe..
      @dontCallMe.. Год назад

      i want to make it when E is pressed...

    • @AaronNuts_
      @AaronNuts_ 9 месяцев назад

      How would i make it so when you press a button, say F, it freezes in the air?

  • @yugsingh2116
    @yugsingh2116 Год назад +4

    where to add camera plane code?

    • @Boxply
      @Boxply  Год назад

      I stated in the video, you don't add it. It runs without addition. It's a calculation script. It runs without being placed onto any object. We reference it to get the coords of where to hang the object from

  • @hugodanielvalladoliddaumas7073
    @hugodanielvalladoliddaumas7073 Год назад +1

    help! it says that joint drive mode mode is obsolete on the Rigidbody script line 90

    • @Boxply
      @Boxply  10 месяцев назад

      Ah. Obsolete. Does the code still run? Maybe its an issue with the unity version. There should be an updated replacement for joint drive mode in the newer versions then, i suggest you use that

  • @hugodanielvalladoliddaumas7073
    @hugodanielvalladoliddaumas7073 Год назад +1

    also, the line isnt tocuhing at all the object im holding, how can i fix this?

  • @thesweetgamer24
    @thesweetgamer24 3 месяца назад +1

    NullReferenceException: Object reference not set to an instance of an object
    DragRigidbody.HandleInputEnd (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidbody.cs:61)
    DragRigidbody.OnMouseUp () (at Assets/DragRigidbody.cs:26)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    whats this

  • @hamzakadah8424
    @hamzakadah8424 Год назад +1

    I need help, the object are just gliding after I touch them, how do i stop it?

    • @Boxply
      @Boxply  Год назад

      Gliding how? I don't understand the situation here.
      Make sure your object has a rigid body and a collider set up just as shown in the video
      Also, make sure that you have the scripts installed. Both of them in the same folder
      If the problem persists. Try it in a new scene & see what ticks (Just set up a character controller by using Dani's tutorial if you want the same scene as me)

    • @hamzakadah8424
      @hamzakadah8424 Год назад +1

      @@Boxply thanks but i figured it out myself i just took away the physics material from the ground.
      Btw do you know a wall walking tutorial that works with danis script?

    • @Boxply
      @Boxply  Год назад

      Glad it worked!
      I have link to a tutorial that does work like a charm on Dani's script. It is made over dani's script.
      ruclips.net/video/Ryi9JxbMCFM/видео.html
      Hope this helps

    • @hamzakadah8424
      @hamzakadah8424 Год назад

      @@Boxply Thank you❤

  • @Aperture_dev
    @Aperture_dev 5 месяцев назад

    i have a strange bug where when i hit play, the camera is separated from my player, causing it to move weird.

  • @VojtaGaming25
    @VojtaGaming25 Год назад +1

    is there a way to make this work with the "E" button because i have my shooting on the left mouse button
    thanks in advance
    great video!

    • @Boxply
      @Boxply  Год назад

      You could modify the code and change the input within it to E. That should be self explanatory. But I'm not sure if there's a function or call for E specifically. You could just put it all in an IF E IS PRESSED Condition then run it unconditional of the left click

  • @yugsingh2116
    @yugsingh2116 Год назад +2

    help
    NullReferenceException: Object reference not set to an instance of an object
    DragRigidBody.HandleInputEnd (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidBody.cs:58)
    DragRigidBody.OnMouseUp () (at Assets/DragRigidBody.cs:23)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

    • @Boxply
      @Boxply  Год назад

      You probably didn't set the tags and etc right.

  • @AdamMohamed-jq8ff
    @AdamMohamed-jq8ff Год назад +1

    its not working with me ( I use new input Systm )
    can I get it work ? I did everything as u but the line is just freezing in ground and it appears if I didnt click anything I tried it in oldeer version and it worked the proplem is from version or input system ? :)

    • @Boxply
      @Boxply  10 месяцев назад

      Probably input system. It should work fine with newer versions

  • @blockyboi942
    @blockyboi942 6 месяцев назад

    CAN SOMEONE PLEASE HELP ME!!!!!!!! It keeps saying jointdrivemode is no longer supported. and its on 2022 editor⚠⚠⚠

  • @starplatinum3305
    @starplatinum3305 Год назад +1

    The line looks weird when u move (fast ?), Did u put that line rendering thing to LateUpdate ?

    • @Boxply
      @Boxply  Год назад +1

      Yeah, I did put in on late update. For me it works completely fine as shown in the video. Have you tried it in an empty project?
      Perhaps some settings are messed up

    • @starplatinum3305
      @starplatinum3305 Год назад

      @@Boxply at 4:46 i guess

  • @lukewerni1
    @lukewerni1 5 месяцев назад

    Hey, so whenever I drag the cube to near to me (the player) I have the ability to fly. I thought about adding a layer to the player and then say that the player layer cannot collide with the interactable. But then I could walk thorugh the interactable while not grabbing it. I mean I could change the layer in the script but do you have a better solution?

  • @viktor-gm8vn
    @viktor-gm8vn 21 день назад

    it is not working it says thre are errors why

  • @benjib20
    @benjib20 4 месяца назад

    yo how do i change the keybind?

  • @frog4000
    @frog4000 2 месяца назад

    for some reason line renderer doesn't make..a line? why might this be?

  • @hugodanielvalladoliddaumas7073
    @hugodanielvalladoliddaumas7073 Год назад +1

    is there anyway I can trigger it with right click?

    • @Boxply
      @Boxply  Год назад +1

      Your have to go into the script and change the input key for that.
      Input.GetKey
      Something like that & change it to whatever the value is for right click or mouse buttob

    • @hugodanielvalladoliddaumas7073
      @hugodanielvalladoliddaumas7073 Год назад +1

      @@Boxply its not letting me change from onmousedrag system to a normal input system, the trail line is not following the object im dragging

    • @Boxply
      @Boxply  Год назад

      Ohh.
      On MouseDrag only works for left click.
      You might have to set up a custom function replicating onmousedrag but for the left key

    • @hugodanielvalladoliddaumas7073
      @hugodanielvalladoliddaumas7073 Год назад

      @@Boxply ty!! ill try!

    • @Boxply
      @Boxply  Год назад

      Gl!

  • @FrostyDevelopment
    @FrostyDevelopment 5 месяцев назад +1

    Uh... the cube just dissapears when i play, but is still there in the editor

  • @bennysensei4662
    @bennysensei4662 10 месяцев назад

    Hi. Im a newbie of that and start to learn Unity. What is your template you using? 3D, VR core, or something? Thanks a lot

    • @Boxply
      @Boxply  10 месяцев назад

      3D

  • @обрубок-г3ц
    @обрубок-г3ц Год назад

    the "JointDriveMode" is no longer supported in DragRigidbody script

    • @Raumfist
      @Raumfist Год назад

      Not sure if you've fixed it by now, but incase anyone else is looking: changing any of the "joint.xDrive = NewJointDrive (force, damping);" Into: "joint.xDrive = new JointDrive(force, damping);" Seems to fix it.

    • @обрубок-г3ц
      @обрубок-г3ц Год назад +1

      @@Raumfist thanks man, but I gave up on unity in general :)

  • @rblxl3g3nd66
    @rblxl3g3nd66 Год назад

    I keep getting the error "NullReferenceException: Object reference not set to an instance of an object". I did everything the same as you did, and I only have one camera. Can you help please?

    • @Boxply
      @Boxply  Год назад +1

      I suggest that you try this tutorial on an empty/test scene to see if it works on your Unity version (it should). You should then, try to see what difference may be causing issues for u

    • @rblxl3g3nd66
      @rblxl3g3nd66 Год назад

      @@Boxply Okay, I'll try that. Thanks!

  • @alkungfu
    @alkungfu Год назад +2

    It isnt working at all

    • @Boxply
      @Boxply  10 месяцев назад +1

      Can you give me a more detailed explaination of the problem you're having? I highly recommend testing this in a new scene and then slowly testing the features with your current game if it causes issues

  • @dontCallMe..
    @dontCallMe.. Год назад

    How can i make it work with prefabs

    • @dontCallMe..
      @dontCallMe.. Год назад

      you cant dump ass ur working with online and not prefabs lol

    • @Boxply
      @Boxply  10 месяцев назад

      Basically you attach the script to the prefab and give it the specific values and tags. It should work just find

  • @ilizzium1241
    @ilizzium1241 10 месяцев назад

    Whose code doesn't work? I rewrote it. write to me and I’ll tell you what to fix

  • @raahimtai
    @raahimtai Год назад +2

    Brilliant. Grabbing Works like a Charm

  • @Bombache7
    @Bombache7 Год назад

    Hey loved the video I was just hoping you could link the music you used during the tutorial to my comment

    • @Boxply
      @Boxply  10 месяцев назад

      All the music I used is by Context Sensitive on RUclips. Look him up! Thanks for taking an interest

  • @Sproute-RL
    @Sproute-RL 10 месяцев назад

    first: when i drag it goesa always to the corner of the object
    second: i can fly

    • @Boxply
      @Boxply  10 месяцев назад

      What's your scene looking like right now? Do you have the tags and stuff set properly?

    • @Sproute-RL
      @Sproute-RL 10 месяцев назад

      @@Boxply ya

    • @Boxply
      @Boxply  10 месяцев назад

      And. What do you mean by "it always goes to the corner of the object?" The line renderer?

    • @Boxply
      @Boxply  10 месяцев назад

      I think the flight part may have to do with your movement controller?

    • @Sproute-RL
      @Sproute-RL 10 месяцев назад

      @@Boxply b ecause u use the gameobject posotion the position is automatiocally at the corner of the object

  • @dontCallMe..
    @dontCallMe.. Год назад

    i want to make it when E is pressed...

  • @ordinarynerdofficial
    @ordinarynerdofficial Год назад

    It doesn't work with my ragdoll character

    • @Boxply
      @Boxply  Год назад

      You probably need to add a new tag to the script. An extra tag/layer that can be picked and dropped specifically for the ragdoll

  • @AlexDiaz-t7e
    @AlexDiaz-t7e Год назад

    doesnt work with ragdolls

    • @Boxply
      @Boxply  Год назад

      Make sure you add a tag/layer for the ragdoll that will allow it to be dragged. Perhaps change its layer/tag the moment the char dies and turns to a ragdoll

  • @royaleyourdad
    @royaleyourdad Год назад

    GOOD TUTORIAL BOIII

  • @Dino-tr6cn
    @Dino-tr6cn Год назад +1

    Hey are u from india ?

    • @Boxply
      @Boxply  Год назад +2

      Well, No I'm not from India. Though, is my accent like that? Haha

    • @Dino-tr6cn
      @Dino-tr6cn 10 месяцев назад

      yea@@Boxply

  • @munyunu
    @munyunu 11 месяцев назад

    Awesome!!

  • @homeholer
    @homeholer 3 месяца назад

    idk why but it just doesn't work lol

  • @jamiewarby3986
    @jamiewarby3986 9 месяцев назад

    its great but outdated now so its pretty scuffed

    • @Boxply
      @Boxply  9 месяцев назад

      Yeah... My bad. Haven't used unity in a while. I picked it up again just a bit ago and will probably refresh myself soon

    • @jamiewarby3986
      @jamiewarby3986 9 месяцев назад

      @@Boxply good for you! that'll be great i subscribed

  • @Shadow_Baconn
    @Shadow_Baconn Год назад

    What is the CameraPlane script for? I keep getting an error "NullReferenceException: Object reference not set to an instance of an object
    DragRigidbody.HandleInputBegin (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidbody.cs:36)
    DragRigidbody.OnMouseDown () (at Assets/DragRigidbody.cs:21)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)" I have no idea what is going on. Whenever I push an object that has the script, it stretches out and looks really weird. I need help.

    • @Shadow_Baconn
      @Shadow_Baconn Год назад

      Also there is no range. the error runs even when i click from far away

    • @Shadow_Baconn
      @Shadow_Baconn Год назад

      I resolved the stretching problem, but not the other problems

    • @Boxply
      @Boxply  Год назад

      The camera plane script is to be able to get the objects position in 3d view according to it's distance from you and the position of the cursor, else, game doesn't know where to hook the object up. It should work tho. Perhaps one of your scripts is interfering. Try it in an empty scene and see what the issue might be

    • @Shadow_Baconn
      @Shadow_Baconn Год назад

      @@Boxply it says that something is wrong with line 36. I got rid of every script using the mouse0 button. Btw I am using a seperate moveplayer script, not the one dani made. it still says that line 36 has something wrong with it. line 36: var ray = Camera.main.ScreenPointToRay(screenPosition); do you think that its because my camera is named differently than yours? plz respond

    • @Boxply
      @Boxply  Год назад

      Maybe you have multiple cameras and it doesn't know which one to use that's supposed to reference the player camera or the camera the player sees through

  • @raelroque5595
    @raelroque5595 3 месяца назад

    stop waste my time bro

  • @Cat_6921
    @Cat_6921 Год назад

    it isn't working there is nothing wrong i tested it on a new scene i only have 1 camera i do have the "Interactive" layer set to the object i want to drag it does have a box collider and it has the DragRididbody script and i have the camera plane script and it doesn't work the only thing it says is this
    NullReferenceException: Object reference not set to an instance of an object
    DragRigidbody.HandleInputBegin (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidbody.cs:36)
    DragRigidbody.OnMouseDown () (at Assets/DragRigidbody.cs:21)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    NullReferenceException: Object reference not set to an instance of an object
    DragRigidbody.HandleInputEnd (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidbody.cs:61)
    DragRigidbody.OnMouseUp () (at Assets/DragRigidbody.cs:26)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
    and i set all of the references
    ok so i changed it and there is only one error first here is the modified code
    using UnityEngine;
    [RequireComponent(typeof(Rigidbody))]
    public class DragRigidbody : MonoBehaviour
    {
    public float force = 600;
    public float damping = 6;
    public float distance = 15;
    public LineRenderer lr;
    public Transform lineRenderLocation;
    Transform jointTrans;
    float dragDepth;
    void OnMouseDown()
    {
    HandleInputBegin(Input.mousePosition);
    }
    void OnMouseUp()
    {
    HandleInputEnd(Input.mousePosition);
    }
    void OnMouseDrag()
    {
    HandleInput(Input.mousePosition);
    }
    public void HandleInputBegin(Vector3 screenPosition)
    {
    var ray = Camera.main.ScreenPointToRay(screenPosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, distance))
    {
    if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Interactive"))
    {
    dragDepth = CameraPlane.CameraToPointDepth(Camera.main, hit.point);
    jointTrans = AttachJoint(hit.rigidbody, hit.point);
    Debug.Log("Joint Attached");
    }
    }
    lr.positionCount = 2;
    }
    public void HandleInput(Vector3 screenPosition)
    {
    if (jointTrans == null)
    return;
    var worldPos = Camera.main.ScreenToWorldPoint(screenPosition);
    jointTrans.position = CameraPlane.ScreenToWorldPlanePoint(Camera.main, dragDepth, screenPosition);
    DrawRope();
    }
    public void HandleInputEnd(Vector3 screenPosition)
    {
    DestroyRope();
    if (jointTrans != null)
    {
    Destroy(jointTrans.gameObject);
    jointTrans = null; // Set jointTrans to null after destroying the game object.
    Debug.Log("Joint Detached");
    }
    }
    Transform AttachJoint(Rigidbody rb, Vector3 attachmentPosition)
    {
    GameObject go = new GameObject("Attachment Point");
    go.hideFlags = HideFlags.HideInHierarchy;
    go.transform.position = attachmentPosition;
    var newRb = go.AddComponent();
    newRb.isKinematic = true;
    var joint = go.AddComponent();
    joint.connectedBody = rb;
    joint.configuredInWorldSpace = true;
    joint.xDrive = CreateJointDrive(force, damping);
    joint.yDrive = CreateJointDrive(force, damping);
    joint.zDrive = CreateJointDrive(force, damping);
    joint.slerpDrive = CreateJointDrive(force, damping);
    joint.rotationDriveMode = RotationDriveMode.Slerp;
    return go.transform;
    }
    private JointDrive CreateJointDrive(float force, float damping)
    {
    return new JointDrive
    {
    positionSpring = force,
    positionDamper = damping,
    maximumForce = Mathf.Infinity
    };
    }
    private void DrawRope()
    {
    if (jointTrans == null)
    {
    return;
    }
    lr.SetPosition(0, lineRenderLocation.position);
    lr.SetPosition(1, this.transform.position);
    }
    private void DestroyRope()
    {
    lr.positionCount = 0;
    }
    }
    and the error is NullReferenceException: Object reference not set to an instance of an object
    DragRigidbody.HandleInputBegin (UnityEngine.Vector3 screenPosition) (at Assets/DragRigidbody.cs:33)
    DragRigidbody.OnMouseDown () (at Assets/DragRigidbody.cs:18)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

    • @HackingStuffIDontKnow
      @HackingStuffIDontKnow Год назад +1

      the camera needs the MainCamera tag to it

    • @Cat_6921
      @Cat_6921 Год назад

      it is@@HackingStuffIDontKnow

    • @Boxply
      @Boxply  10 месяцев назад

      Sorry for being a bit late but may have to do with the tags on the objects in the scene that you want to pick and drop