EASILY Pick up and Throw Objects | Unity 2018 Tutorial

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

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

  • @mattwester9093
    @mattwester9093  Месяц назад

    This video is now six years old and obsolete! Check out the new 2024 Unity 6 updated version: ruclips.net/video/fApXEL0xsx4/видео.html

  • @yalcngovyaprak8430
    @yalcngovyaprak8430 5 лет назад +133

    Code;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Pickup : MonoBehaviour {
    float throwForce = 600;
    Vector3 objectPos;
    float distance;
    public bool canHold = true;
    public GameObject item;
    public GameObject tempParent;
    public bool isHolding = false;
    // Update is called once per frame
    void Update () {
    distance = Vector3.Distance (item.transform.position, tempParent.transform.position);
    if (distance >= 1f)
    {
    isHolding = false;
    }
    //Check if isholding
    if (isHolding == true) {
    item.GetComponent ().velocity = Vector3.zero;
    item.GetComponent ().angularVelocity = Vector3.zero;
    item.transform.SetParent (tempParent.transform);
    if (Input.GetMouseButtonDown (1)) {
    item.GetComponent ().AddForce (tempParent.transform.forward * throwForce);
    isHolding = false;
    }
    }
    else
    {
    objectPos = item.transform.position;
    item.transform.SetParent (null);
    item.GetComponent ().useGravity = true;
    item.transform.position = objectPos;
    }
    }
    void OnMouseDown()
    {
    if (distance

  • @kathrynprater2767
    @kathrynprater2767 3 года назад +1

    Thank you for this!! There are so many videos for this concept but they either only work for their code or do not show you the full code. Also, I have tried many with so many bugs. This is perfect and very adaptable.

  • @RyanRioga
    @RyanRioga 5 лет назад +3

    Great tutorial! I was super stumped on this and there was a few spots I completely forgot about or just didn't know. Super helpful and I was able to pick up my boxes in my project perfectly. Nice work!

  • @SamuelVirkola
    @SamuelVirkola 5 лет назад

    I've tried to search for stuff like this for so long. I am very surprised I didn't come across this tutorial. This video was perfect (for me at least). Thank you.

  • @umpteenthreason9627
    @umpteenthreason9627 5 лет назад +2

    Great tutorial, with a few minor tweaks it even works fine in a 2D game.

  • @liam-iq3ji
    @liam-iq3ji 6 лет назад

    Your awesome thanks to much, the way you program is better to watch than anything I have ever found online thanks for helping so much. been hoping to make a game that can do this and I was having so many problems.

  • @connorpayne2671
    @connorpayne2671 4 года назад

    This is exactly what I was looking for. I combined this with what I had already and added my own keys for things such as dropping and sticking the item in the air and it does exactly what I want. Thank you
    Now I just need to figure out how to make the object go back to its parent when it is moved around, like in Skyrim 😅

    • @mattwester9093
      @mattwester9093  4 года назад +1

      Glad the tutorial helped you out!

    • @XnathOW
      @XnathOW 4 года назад +1

      Hey; You can try this:
      public GameObject originalParent;
      void Awake()
      {
      //Setting the original parent in the start of the scene
      originalParent = gameObject.transform.parent;
      }
      void OnMouseUp()
      {
      isHolding = false;
      //Setting the gameObject back under the original parent after letting go of it.
      gameObject.transform.parent = originalParent;
      }

    • @connorpayne2671
      @connorpayne2671 4 года назад

      @@XnathOW I'll have to have a look to see if that does the job. Thank you

  • @lairlair2
    @lairlair2 4 года назад +4

    Super helpful thank you!!
    Two ideas to improve the code (maybe?):
    - declaring a rigidbody at the beginning and define it in the awake function to avoid typing "item.GetComponent ()." a couple of times.
    private Rigidbody rb;
    private void Awake()
    {
    rb = item.GetComponent();
    }
    - Use colliders set as triggers instead of distance. It allows you to pick up objects of different sizes and shapes. The downside is you'll need to set up a trigger for every single object. I'll have to test that one

  • @Zharkan16
    @Zharkan16 3 года назад +1

    I parent it to the cameras transform but it stops randomly in space when I walk around

  • @skitterlitter
    @skitterlitter 4 года назад

    finally a tutorial that works THANK YOU

    • @mattwester9093
      @mattwester9093  4 года назад

      Glad it helped!

    • @mattwester9093
      @mattwester9093  4 года назад

      @Conshe Tumare are you getting any errors that I can help out with?

    • @fast07hzmc401
      @fast07hzmc401 3 года назад

      @@mattwester9093 Hello im not getting any errors but for some reason i can't really pick up the cube, i can only drag it on the ground. Might you know what is wrong ?

    • @vini8v8
      @vini8v8 2 года назад

      ​@@fast07hzmc401 Checks if the 'Guide' object is a child of your main camera. If it is the child of some other object (like your player, if your camera is not its child) the problem is there.
      The Guide must be a child of the camera.
      It works, but I need to find another way to do this, more efficient... :/

  • @ONIVSTA
    @ONIVSTA 3 года назад +1

    WHAT DO YOU RPESS TO PICK IT UP

    • @mattwester9093
      @mattwester9093  3 года назад

      If you followed the video then press and hold the left mouse button to pick it up.

  • @ChrisBurns3D
    @ChrisBurns3D 6 лет назад

    Great tutorial mate, please keep making more

  • @fast07hzmc401
    @fast07hzmc401 3 года назад

    Man you got to have a tutorial on the first person puzzle game you made (:

  • @toslytodie
    @toslytodie 5 лет назад +9

    when i pick up object with this script it's deforms!

  • @cyantifical
    @cyantifical 6 лет назад +1

    Thank you so much man! Helped out loads

  • @baxterclagmoar9333
    @baxterclagmoar9333 5 лет назад +1

    Can pick up objects but they stretch and deform whilst held??? No idea why. Works in a brand new fresh scene, but not in the scene I need it in.

  • @aayushshrestha2871
    @aayushshrestha2871 4 года назад +1

    It was very helpful. But I just got a problem that whenever I pick up the object and try to move around the object remains in the same position.
    Surprisingly, it works on the standard FPS controller of the unity but not in the custom fps controller I made.
    All the things work perfectly except the move around with the object part.
    Just found out that it only works on the fps controller with character controller, not on the fps controller with rigid body.
    Is there any way to solve this problem?

    • @kcatthirtysix
      @kcatthirtysix 4 года назад

      I’m hoping he responds, I’m having the same issue

  • @stevengibson4773
    @stevengibson4773 5 лет назад +2

    Please add your scripts in the description. Helpful for us looking at this tutorial in later years to ensure we aren't wasting our time if it doesn't compile

    • @XnathOW
      @XnathOW 4 года назад

      This code is so basic that there shouldnt be any issues tbh.

    • @stevengibson4773
      @stevengibson4773 4 года назад

      ​@@XnathOW The point of snippet is so people can check that the code still works without copying line by line. A big waste of time. As time goes on APIs change, and even now my comment is 7 months old which means even more likely that this code will not work as is.

  • @vanessanajem6491
    @vanessanajem6491 6 лет назад +1

    How are you testing the game? I am moving using the arrows to step forward and backward and the mouse to turn directions, but i can't click on the object. Can you help please?

  • @mikethegamedev
    @mikethegamedev 4 года назад

    thanks so much! i learned a new knowledge

  • @AZ-qw1kz
    @AZ-qw1kz 6 лет назад

    If it isn't working for you, this is what fixed it for me.
    Make sure the "if (distance

  • @ThatGuyDownInThe
    @ThatGuyDownInThe 5 лет назад +1

    If I already have something else mapped to the mousebutton what should I do?

  • @realmcraftchronicles
    @realmcraftchronicles 2 года назад

    Thank you for this! So, would you really want the throw and pickup to be in the same class? Or would they be separate classes?

  • @salemisjuucy
    @salemisjuucy 4 года назад +2

    when i move the camera the object doesnt move with it, it stays on the floor, can someone please help asap

    • @northdiddles
      @northdiddles 4 года назад

      You need to put the guide on the camera so it rotates with the camera.

    • @salemisjuucy
      @salemisjuucy 4 года назад

      Wilkie Noob but when I do that when I look up the cube distorts and and changes shape, i haven’t been able to find a fix for that just yet.

    • @northdiddles
      @northdiddles 4 года назад

      @@salemisjuucy
      Im working on a fix. I think its the rotation and scale of the object. Idk yet

    • @northdiddles
      @northdiddles 4 года назад

      @@salemisjuucy
      Haven't found ought

    • @northdiddles
      @northdiddles 4 года назад

      @@salemisjuucy
      Ill work on it later

  • @ethanjarveyocampo1199
    @ethanjarveyocampo1199 4 года назад +1

    This looks great but I have problem that I can't pick it up :(. It just moves along the ground..... Is the first person controller strictly necessary?

    • @Nuff.x
      @Nuff.x 4 года назад +1

      well kind of, the problem you may have is that the guide is not a child object of the camera as id assume you have it set as the guide as the childed object of the actual character. so honestly you could try making the guide a child object of your third person camera but I don't think you would get the results you are looking for

    • @ethanjarveyocampo1199
      @ethanjarveyocampo1199 4 года назад +1

      @@Nuff.x Yeah I figured that out kinda late, but I finally got it! :) thanks for the tip man

    • @Nuff.x
      @Nuff.x 4 года назад

      @@ethanjarveyocampo1199 np man

  • @larrynachos
    @larrynachos 4 года назад +1

    Thank you so much for this tutorial! One problem though... with some bigger objects, I can lift the object and rotate myself with it, but as soon as I use WASD to move around, the object and pickup guide remain where they are, only moving in relation to rotation. I can pick up and carry a ball with me just fine, but larger crates won't move. Any ideas? Thanks again!

    • @jackjames5519
      @jackjames5519 4 года назад

      following this for answers

    • @smifyy
      @smifyy 4 года назад

      bit late but I added this and it worked for me:
      item.GetComponent().constraints = RigidbodyConstraints.FreezePosition; when you pick up the object and
      item.GetComponent().constraints = RigidbodyConstraints.None; when you let go or throw it

    • @bomdiabatata
      @bomdiabatata 3 года назад

      @@smifyy added where?

  • @Yunghamster12
    @Yunghamster12 3 года назад

    why does it not follow well on the x axis and not at all while moving

  • @gilgusinsky2513
    @gilgusinsky2513 4 года назад +1

    is there a way to do this with 2d, i mean be able to "pick up" a sprite and throw it around with ur mouse? i have a script for the pick up where mouse down moves the sprite, but i dont know how to send it flying in the direction i "throw it."

    • @XnathOW
      @XnathOW 4 года назад

      Depends on "how" you're trying to throw it?
      Swinging your mouse?
      Or is it held by a character that throws it at the direction of the cursor?
      For the latter you need to find the directional vector that is formed between the player and cursor, and use that Vector in the AddForce function (instead of the tempParent.transform.forward) that is being used in this tutorial.

  • @genuinefraud8262
    @genuinefraud8262 6 лет назад +4

    UnassignedReferenceException: The variable item of PickUp has not been assigned.
    You probably need to assign the item variable of the PickUp script in the inspector.
    PickUp.Update () (at Assets/PickUp.cs:37)
    EDIT: oh nvm

  • @mrsein
    @mrsein 4 года назад +1

    Works GREAT! THX!!!

  • @rbanister100
    @rbanister100 2 года назад

    When making video's, you need to make sure the INS (the INSERT KEY), is NOT pressed, to get rid of the BLOCK CURSOR. in Visual Studio.

  • @GrimBirthday
    @GrimBirthday 5 лет назад

    Wow really fun and easy thank you

  • @fast07hzmc401
    @fast07hzmc401 3 года назад

    Like uh i don't really know what i did wrong. I can only drag the object on the ground and not really pick it up in the air and move it. Does someone know what's wrong ?

  • @xxRobodragonxx
    @xxRobodragonxx 4 года назад +1

    For some reason when i bump into the object that i am picking up it wont work at all. but when i get out of the distance area it will work, could this have to do with my game being a 3rd person not a first person game.

  • @TheOfficialShortStuff
    @TheOfficialShortStuff 3 года назад

    The cube doesn't look up with me. Any ideas?

  • @jamesqueen2673
    @jamesqueen2673 4 года назад

    Hello,
    Is there any way to pick up the item without it rotating? i notice when i pick up the item it rotates based off of where i'm looking with the mouse. I want my object to stay flat when i pick it up not rotate on the y axis

  • @prathmeshthakkar4563
    @prathmeshthakkar4563 5 лет назад +1

    Hey matt,
    Great tutorial! But I am having a problem in which it can detect the mouse presses (and checks isholding) but the object deosn't move. Any ideas?

    • @BT-cp4lq
      @BT-cp4lq 5 лет назад +2

      same is happening to me I have no idea why

  • @MayurBhagat10
    @MayurBhagat10 6 лет назад

    Thanks for nice tutorial.
    You can update tutorial with just two things.
    1)If pickable item is in range then show UI prompt like some sign that item is ready to be picked up.I guess enabling 2d ui image part of canvas makes sense.
    2) Item should not be dropped automatically with coliding issue. We should resist it in some way. You can decide how to handle it.
    Thank You !

  • @JOHNSMITH-sj3lg
    @JOHNSMITH-sj3lg 3 года назад

    the script dosent work for me i have no errors and make all the stuff u make can u help pls?

  • @Andrewcompton22
    @Andrewcompton22 5 лет назад

    Do you have a video or can you make a video on how to do this with a 2D game? or do I just need to change it to Vector2's? or can I just use this to achieve the same on 2D? lol sorry for all the questions

  • @kingdom216
    @kingdom216 3 года назад

    How does the script change when one is using a third person controller?

  • @Laatikkoz
    @Laatikkoz 2 года назад

    pls tell me what is wrong in script

  • @ancolia8772
    @ancolia8772 3 года назад

    yeah but i want the player to be able to hold multiplayer objects. how you do that?

  • @farfadet46
    @farfadet46 6 лет назад

    hi thanks you so mush for this good tutorial. but the "canhold" variable isn't used ? have you a second video in WIP ? ;)

  • @Assniffer
    @Assniffer 5 лет назад

    Will this works on enemy's as well like if i want to grab them then chokeslam them like in wrestling game's?

  • @temp7720
    @temp7720 4 года назад +2

    Thank you for code. I got it working but I have one small problem.
    When I look up my box goes up to far and then gets flattened.
    It does not stay nicely in view like your demo.
    Any thought?
    Thank you agen.

    • @northdiddles
      @northdiddles 4 года назад

      Im working on a fix ill let you know later.

  • @Maxspeedtofast
    @Maxspeedtofast 6 лет назад +9

    wished you had made screen shots of the scripting or just added to the description

  • @ShootLuckGaming
    @ShootLuckGaming 5 лет назад +5

    When I pick up my object i can move it around, it will only spin with me, when i move forward or backward it will remain in place, help please

    • @yunussutanti2788
      @yunussutanti2788 5 лет назад +3

      item.transform.position = tempParent.transform,position;
      item.transform.rotation = tempParent.transform.rotation;
      it's should works fine, but it may look more unrealistic

    • @yunussutanti2788
      @yunussutanti2788 5 лет назад +1

      +Shoot Luck Gaming

    • @TheDynamicDudes
      @TheDynamicDudes 5 лет назад

      @@yunussutanti2788 where in the code do you put this?

    • @TheDynamicDudes
      @TheDynamicDudes 5 лет назад +1

      @@yunussutanti2788 nvm i just put the guide as a child of my camera rather than my character controller and it works fine

    • @Nuff.x
      @Nuff.x 4 года назад

      @@TheDynamicDudes bruh i had a similar ish problem and making the camera the guides parent solved it fro me thank you!!

  • @EmuTheEmo
    @EmuTheEmo 6 лет назад +1

    Everything works. But just 1 issue. When I walk around the object stays in the same place. I can move it by looking around but if I try to move it won't move with me.
    No errors in the code and it's the same as yours. I've typed the code out a few times and still has the same issue

    • @FuffoloWorks
      @FuffoloWorks 6 лет назад

      Same! Mine appears also to be working only on the x axis if i look around

    • @brillerment
      @brillerment 6 лет назад +1

      @@FuffoloWorks Make sure your guide GameObject is a child of FirstPersonCharacter, and not FPSController. That should fix that.

    • @forte9300
      @forte9300 6 лет назад

      Brent Miller Thanks so much that worked! The object doesn’t throw though..

  • @alexanderthegreat7462
    @alexanderthegreat7462 4 года назад

    how to adjust how close the player can interact with the object?

    • @mattwester9093
      @mattwester9093  4 года назад +1

      Change the distance float in the script

  • @michaelberger4942
    @michaelberger4942 5 лет назад

    Hi thanks for this tut..., works, but when i start Gamemode the rigidbody is picked up and cant be dropped... what's wrong?

  • @BradyHege
    @BradyHege 4 года назад +1

    Hey, I'm curious what I can do (if anything) to prevent my object from warping after being picked up. I'm testing this code on a simple sphere and it turns into basically an egg. What am I doing wrong? Thanks in advance to anyone who can help!

    • @awesomepotato292
      @awesomepotato292 4 года назад

      I have the same problem. As far as I know, the problem is because one of the parents of the guide has some scaling and it causes that. I'm still looking for a fix that allows me to scale my player.

  • @flymilk1
    @flymilk1 4 года назад

    it's not working it says I have errors like expected. insert a semicolon at the end I looked and it has a semicolon.

  • @iuhchiuhhgbnr
    @iuhchiuhhgbnr 4 года назад

    Good tutorial thankyou

  • @hackerhd9203
    @hackerhd9203 6 лет назад +4

    every time i move the object the scale is changing can someone help me

    • @rafioz0
      @rafioz0 5 лет назад +3

      I also have this problem :

    • @Maggiethegsd
      @Maggiethegsd 4 года назад

      @@rafioz0 press w and then move it...

  • @ZombiehuntersComeback
    @ZombiehuntersComeback 5 лет назад

    Hi great tutorial on how to pickup and throw items I was looking for this for a while now. I have one problem though. When I pickup an item the item goes through another object instead of colliding with it. I've checked and detectCollisions is set to true. I've pasted the code under here so you could tell me what I did wrong.
    float throwForce = 600;
    Vector3 objectPos;
    float distance;
    public bool canHold = true;
    public GameObject item;
    public GameObject tempParent;
    public bool isHolding = false;
    // Update is called once per frame
    void Update()
    {
    distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
    if(distance >= 1f)
    {
    isHolding = false;
    }
    //Check if isholding is true
    if (isHolding)
    {
    //Picks up item and holds it in front of you
    item.GetComponent().velocity = Vector3.zero;
    item.GetComponent().angularVelocity = Vector3.zero;
    item.transform.SetParent(tempParent.transform);
    item.transform.position = tempParent.transform.position;
    item.transform.rotation = tempParent.transform.rotation;
    //Checks if the right mouse button has been pressed
    if (Input.GetMouseButtonDown(1))
    {
    item.GetComponent().AddForce(tempParent.transform.forward * throwForce);
    isHolding = false;
    }
    }
    else
    {
    //leaves the picked up item at it's last position if isHolding is false
    objectPos = item.transform.position;
    item.transform.SetParent(null);
    item.GetComponent().useGravity = true;
    item.transform.position = objectPos;
    }
    }
    void OnMouseDown()
    {
    if(distance

  • @kingbrodan
    @kingbrodan 6 лет назад

    what if you have multiple players in the scene, like a multiplayer game how do you account for the different cameras?

  • @pepperj
    @pepperj 6 лет назад

    Hey great tutorial...Question I have: I can pick up and throw but after that I can't pick it up again when I walk over to it. Any Ideas? Thanks!

  • @toastedcracker1499
    @toastedcracker1499 4 года назад

    I have a question what is the guide?

  • @Alex-vu5uu
    @Alex-vu5uu 6 лет назад +5

    hi, can you explain the function to use the key E instead of on mouse down? thanks a lot!

    • @mattwester9093
      @mattwester9093  6 лет назад +5

      Alex x instead of the function OnMousdDown, use OnMouseOver and then add an additional line of code right under that saying if(Input.GetKeuDown(KeyCode.E))
      Then put the rest of the code down here. Hope that helps

  • @VrMurdoll
    @VrMurdoll 4 года назад

    Do u know anyway to make it toggle with E both to pick up and place down

    • @VrMurdoll
      @VrMurdoll 4 года назад

      i already got it to pick up with E but i also want it place down with it

    • @mattwester9093
      @mattwester9093  4 года назад

      You could always try making a bool variable, something like toggleOn, and when you press E, add an if statement checking if toggleOn == true or not.

    • @VrMurdoll
      @VrMurdoll 4 года назад

      @@mattwester9093 i probably shouldn't have said toggle. i just mean make E pickup and make E drop

    • @mattwester9093
      @mattwester9093  4 года назад

      @@VrMurdoll a toggle would work fine for that. For example by default toggleOn == false. Then when you press E, run a check. If toggleOn == false, pick up the item, and switch toggleOn to true. Have another statement when E is pressed checking if toggleOn == true, and if it is, drop the item.

    • @VrMurdoll
      @VrMurdoll 4 года назад

      @@mattwester9093 ive got it to have it change to true but i cant get it to change to false

  • @gamesfox8768
    @gamesfox8768 5 лет назад

    I have a problem, my item won't move up or down.

    • @gamesfox8768
      @gamesfox8768 5 лет назад

      nvm, parented to camera and worked. thanks for the awesome tutorial!

    • @Walllam
      @Walllam 5 лет назад

      @@gamesfox8768 couldnt figure out why it didnt work for me, thanks for telling me how you fixed it

  • @BlanketsWilson
    @BlanketsWilson 6 лет назад

    Hey what's up, this is great!
    Is there any way to change it where to pick up the object with the lmb and also throw it with the lmb?
    Also, I've been trying to change the distance so the player can pick up the object when closer to it but I feel like it's getting further away.
    Talk soon,

    • @mattwester9093
      @mattwester9093  6 лет назад +1

      Brian Wilson for the distance could it be that your < is reversed? As for using left mouse button to pick up and throw, try something like this(untested code, probably won't copy over perfectly):
      bool isHolding = false;
      Void OnMouseOver
      If(input.getmousebuttondown(0))
      If(isHolding == false)
      IsHolding = true
      Else if(isHolding == true)
      //put throw code here
      Hope you get the general idea of how that would work. Sorry it's not exact code but in writing this from my phone.
      Happy coding,
      Matt

  • @lousev3485
    @lousev3485 5 лет назад

    i cant raise or lower the cube any idea how to fix that

    • @XnathOW
      @XnathOW 4 года назад

      You need to parent the object to player camera or object under the camera

  • @crimson3393
    @crimson3393 4 года назад

    The object doesnt move with me when i walk around, any ideas?

    • @rodovicky7534
      @rodovicky7534 4 года назад

      yeah same thing is happening with me tooo

  • @Ariel-uo7le
    @Ariel-uo7le 11 месяцев назад

    thank you so muchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @mumiaminteh6786
    @mumiaminteh6786 4 года назад

    Can i get the first person controller code?

    • @mattwester9093
      @mattwester9093  4 года назад

      Hey, sorry, but the video is pretty outdated now, and I have not done much programming in the last couple of years. I don't have access to the fps controller script, as I believe Standard Assets is no longer supported.. I know this video is pretty outdated as well, so I will see about updating it with a new FPS controller

  • @beefbossfromwiisports
    @beefbossfromwiisports 4 года назад

    how do you make it work when you press a certain key?

    • @mattwester9093
      @mattwester9093  4 года назад

      As I mentioned in the video, you would want to use the function OnMouseOver instead of OnMouseDown. Then inside the function you would check for the input key you want.

    • @beefbossfromwiisports
      @beefbossfromwiisports 4 года назад

      Matt Wester okay, I didn’t understand what you meant at first but now that I see it in text I understand, I guess my brain just works like that:) thanks for your help!

    • @mattwester9093
      @mattwester9093  4 года назад +1

      @@beefbossfromwiisports glad I could help. Any other questions just let me know!

    • @beefbossfromwiisports
      @beefbossfromwiisports 4 года назад

      @@mattwester9093 Here's my script:
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class FancyPickUP : MonoBehaviour {
      float throwForce = 600;
      Vector3 objectPos;
      float distance;
      public bool canHold = true;
      public GameObject item;
      public GameObject tempParent;
      public bool isHolding = false;
      // Update is called once per frame
      void Update () {
      distance = Vector3.Distance (item.transform.position, tempParent.transform.position);
      if (distance >= 1f)
      {
      isHolding = false;
      }
      //Check if isholding
      if (isHolding == true) {
      item.GetComponent ().velocity = Vector3.zero;
      item.GetComponent ().angularVelocity = Vector3.zero;
      item.transform.SetParent (tempParent.transform);
      if (Input.GetMouseButtonUp (1)) {
      item.GetComponent ().AddForce (tempParent.transform.forward * throwForce);
      isHolding = false;
      }
      }
      else
      {
      objectPos = item.transform.position;
      item.transform.SetParent (null);
      item.GetComponent ().useGravity = true;
      item.transform.position = objectPos;
      }
      }
      void OnMouseUp()
      {
      if (distance

  • @kennethbhasme9094
    @kennethbhasme9094 4 года назад

    Can someone please share link as to where I can get fps controller asset

    • @kingdom216
      @kingdom216 3 года назад

      unity assets/ standard assets

  • @piertFB
    @piertFB 5 лет назад

    are you using a vr tool? or just using the computer to do that movements?

  • @gray525
    @gray525 4 года назад

    When i try and add it it gives my the classname and file name do not match

  • @kasinduudara4040
    @kasinduudara4040 6 лет назад

    I got a Unassigned Refarance Exception this line objetpos = items.transform.position;
    what should have to do

    • @mattwester9093
      @mattwester9093  6 лет назад

      kasindu udara I think you just need to remove the s in items, as in the script it is defined as item. Hope that helps!

    • @kasinduudara4040
      @kasinduudara4040 6 лет назад

      not working , it is not compilation error it is run time error

    • @mattwester9093
      @mattwester9093  6 лет назад

      kasindu udara then it sounds like you may have forgotten to assign your variables?

  • @bobby-goody
    @bobby-goody 5 лет назад

    every time i pick up my object and look around it moves and the scale changes the object deforms itself and wont stay in the middle of the screen like yours can u help me please?

    • @baxterclagmoar9333
      @baxterclagmoar9333 5 лет назад

      Same problem here... It worked fine when I created a brand new scene, but doesn't work in the scene I need it to... Really frustrating.

    • @bobby-goody
      @bobby-goody 5 лет назад

      JuBable okay cheers mate

  • @maksous
    @maksous 6 лет назад

    Great tutorial, but in my case something is wrong. I have a character, camera attached to it and then guide attached to camera. Basically, I can't pick up item - by left clicking, but when I left click on it, the scale of item seems to be changing, and when I right click while holding left button - it actually throws the item and holds it (I can't drop it tho).
    Got any idea how to fix this? :D

    • @mattwester9093
      @mattwester9093  6 лет назад

      maksous1 be sure that in the OnMouseDown function that you are changing the transorm.position, as I have heard that only changing the .transform will edit the scale. Hope that helped!

    • @maksous
      @maksous 6 лет назад

      In OnMouseDown method I have:
      if(distance = 1f)
      {
      isHolding = false;
      }
      if (isHolding == true)
      {
      item.GetComponent().velocity = Vector3.zero;
      item.GetComponent().angularVelocity = Vector3.zero;
      item.transform.SetParent(tempParent.transform);
      if (Input.GetMouseButtonDown(1))
      {
      item.GetComponent().AddForce(tempParent.transform.forward * throwForce);
      isHolding = false;
      }
      else
      {
      objectPos = item.transform.position;
      item.transform.SetParent(null);
      item.GetComponent().useGravity = true;
      item.transform.position = objectPos;
      }
      }
      }
      Basically, I really have no clue what is wrong :P

    • @mattwester9093
      @mattwester9093  6 лет назад

      Could you list any errors you are getting? At first glance your script seems fine

    • @maksous
      @maksous 6 лет назад

      Matt Wester yeah, thats the thing - no errors whatsoever. Just not working :|

    • @mattwester9093
      @mattwester9093  6 лет назад

      Did you remember to assign all the variables in the inspector?

  • @paulschmidt1689
    @paulschmidt1689 6 лет назад

    Everytime I playtest isHolding always starts with the box checked for true and I can't uncheck it any help?
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PickUp : MonoBehaviour {
    float throwForce = 600;
    Vector3 objectPos;
    float distance;
    public bool canHold = true;
    public GameObject item;
    public GameObject tempParent;
    public bool isHolding = false;
    // Update is called once per frame
    void Update () {
    //Check if isHolding
    if (isHolding = true) {
    item.GetComponent ().velocity = Vector3.zero;
    item.GetComponent ().angularVelocity = Vector3.zero;
    item.transform.SetParent (tempParent.transform);
    }
    if (Input.GetMouseButtonDown(1))
    {
    }
    }
    else {
    objectPos = item.transform.position;
    item.transform.SetParent (null);
    item.GetComponent().useGravity = true;
    }
    }
    void OnMouseDown ()
    {
    isHolding = true;
    item.GetComponent().useGravity = false;
    item.GetComponent().detectCollisions = true;
    }
    void OnMouseUp(){
    isHolding = false;
    }

    • @XnathOW
      @XnathOW 4 года назад

      In your if it needs to be
      if (isHolding == true), 2x == to check

  • @tranvanhoang5377
    @tranvanhoang5377 4 года назад

    How can i controll it?

  • @Luckypants2022
    @Luckypants2022 4 года назад

    Still works?? Dosent for me

  • @Moonriderr
    @Moonriderr 4 года назад

    Yo, i was searching something in stackoverflow and i saw u tutorial there.I saw some guy telling u its not good to update some value in the loop, but i cannot simply find the topic anymore.Can u post the optimized script here? Thanks u! :)

  • @Cronoss95
    @Cronoss95 6 лет назад

    Heyho! Your script looks great, anyway I am not able to pick anything up! I wrote the script 3 times now, and I am still not able to move anything. Wich button do I have to press? :(

    • @mattwester9093
      @mattwester9093  6 лет назад

      R Musterman this script uses the left mouse button to pick up, but you have to hold it down. To throw the object you have to press the right mouse button.

    • @Cronoss95
      @Cronoss95 6 лет назад

      Ahh, i found the problem! I changed the values of the distance from 1f to 50 and now everything works (well, I cant lift anything above my head, but at least this works now :D) - do you know why this is happening? Thank you for your reply!

    • @mattwester9093
      @mattwester9093  6 лет назад

      R Musterman I had a similar issue when I was updating the turorial, for me it was as simple as making sure my guide object was a child to the camera object of your player, if you are using standard assets I think it is called FPSCharacter or something like that

  • @kerodical9237
    @kerodical9237 6 лет назад

    Well, how if I were to use this on multiple objects? I mean how about performance?

    • @lucianopereira3612
      @lucianopereira3612 6 лет назад

      It will be pretty bad since he's checking the distance every frame regardless if anything is being held or if anything has a mouse over it. You should update his code to only update the distance variable only if isHolding is true and also down at the OnMouseDown function.
      You should also not do getComponent() so many times- do it once and save it to a variable (preferrably do this in a Start() method in the script).

  • @accesmadagascar
    @accesmadagascar 4 года назад

    im getting error CS0101 how do you fix it?

  • @hendryvarianto
    @hendryvarianto 6 лет назад +1

    Everytime i grabbed the object,
    it'll fall slowly ..
    help ?

    • @XnathOW
      @XnathOW 4 года назад

      Forgot to disable RigidBody.Gravity

  • @mr_kwkd
    @mr_kwkd 4 года назад

    uhm does anyone know why when i pick it up the object stretches itself? I know this is 8 months old but if anyone sees this can you help?

    • @XnathOW
      @XnathOW 4 года назад

      It is because you put it as a child of your player, and your player is stretched (Scale), it copies the same amount of scale.
      The object holding the item needs to be at 1.0 scale in all Axis

  • @5ManaAndADream
    @5ManaAndADream 4 года назад +1

    this is cool, but you cannot jump, the ball will not follow you vertically.

  • @youknov4748
    @youknov4748 6 лет назад +1

    Hello, I did everything like in the video but when I try to throw the object it doesn't throw forward, it throws sidewards (to the right side). No matter how I rotate myself it throws to the right side. What can I do?

    • @mattwester9093
      @mattwester9093  6 лет назад

      Melon Intros you could try changing the direction of the thrust / throw. I think something like Vector3.left instead of Vector3.forward should work fine.

    • @youknov4748
      @youknov4748 6 лет назад

      Matt Wester Hmm I gotta look but I can't remember a Vector.right or something, maybe I have to rotate my empty?

    • @youknov4748
      @youknov4748 6 лет назад

      je I am stupid the z axis pointed to the right side xD so I fixed it and it works! Thanks!

  • @brettpitmanii9102
    @brettpitmanii9102 5 лет назад

    what about 3rd person controller?

  • @darcygibbons2891
    @darcygibbons2891 5 лет назад

    Thanks so much!

  • @starstorms21
    @starstorms21 6 лет назад

    hey dude my cube isnt moving in the y axis at all, any ideas?

    • @starstorms21
      @starstorms21 6 лет назад +1

      nevermind i didnt parent it to the camera Xp

    • @mattwester9093
      @mattwester9093  6 лет назад

      Yea i had that problem too setting it up, glad you figured it out

    • @starstorms21
      @starstorms21 6 лет назад

      btw do you know a way i can make the object move closer to the player using the scroll wheel? or maybe rotating it using Q & E

    • @starstorms21
      @starstorms21 6 лет назад

      thank god i found this tutorial, it works like a charm after the parenting issue being fixed

  • @superniall9995
    @superniall9995 6 лет назад +1

    I noticed when I move objects they change in scale

    • @superniall9995
      @superniall9995 6 лет назад

      I figured it out. You need to make sure the guides scale is 1, 1, 1 and not something like .3, 1, .3.

    • @hackerhd9203
      @hackerhd9203 6 лет назад +1

      @@superniall9995 i tried what you said but ist still not working the scale is changing every time i move the object

    • @rafioz0
      @rafioz0 5 лет назад +1

      @@hackerhd9203 me too

    • @thebigcheese1153
      @thebigcheese1153 4 года назад

      Hacker hd try the scale of the camera I worked for me

    • @thebigcheese1153
      @thebigcheese1153 4 года назад

      Rafiozo try the scale of the camera

  • @hendryvarianto
    @hendryvarianto 6 лет назад +1

    helppp
    everytime i clicked on the cubed, it changes size...

    • @rafioz0
      @rafioz0 5 лет назад +1

      @@uremi. I also have this problem :

    • @rafioz0
      @rafioz0 5 лет назад +1

      @@uremi. using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class ThrowObject : MonoBehaviour
      {
      public Transform player;
      public Transform playerCam;
      public float throwForce = 10;
      bool hasPlayer = false;
      bool beingCarried = false;
      public int dmg;
      private bool touched = false;
      void Update()
      {
      float dist = Vector3.Distance(gameObject.transform.position, player.position);
      if (dist

    • @vini8v8
      @vini8v8 5 лет назад

      I had the same problem, in my case the problem was: my camera has its Scaling changed (1, 0.7, 1) I fixed it to (1, 1, 1) and it worked.

    • @XnathOW
      @XnathOW 4 года назад

      @@uremi. Not about the code; Scaling of the parent will be transferred to the lifted object.

    • @XnathOW
      @XnathOW 4 года назад +1

      @@rafioz0 Not about the code; Scaling of the parent will be transferred to the lifted object.

  • @silaschristensen8882
    @silaschristensen8882 6 лет назад +10

    Doesnt work, sadly :(

    • @MootzartOfficial
      @MootzartOfficial 6 лет назад

      Same here...

    • @BT-cp4lq
      @BT-cp4lq 5 лет назад

      @@gabe9040 that doesn't work for me and neither does the tutorial

    • @BT-cp4lq
      @BT-cp4lq 5 лет назад

      @@gabe9040 yes I did

    • @BT-cp4lq
      @BT-cp4lq 5 лет назад

      Gabe it simply doesn’t work

    • @BT-cp4lq
      @BT-cp4lq 5 лет назад

      @@gabe9040 yes I did it all and I can rad the console.

  • @Powerpuffsama
    @Powerpuffsama 5 лет назад

    Followed the tutorial exactly and it doesn't even work. Can't even pick up objects.

  • @joshuatran3070
    @joshuatran3070 6 лет назад

    When I try to pick it up, it disappears. Any fixes on this?

    • @XnathOW
      @XnathOW 4 года назад

      Id guess the child object you placed under the camera is somewhere else other than on the player? Or it has a rigidbody and isnt kinematic

  • @Paaskedrengen
    @Paaskedrengen 4 года назад

    I just cant make this to work! Anyone have a updated script? Preferably with pressing a button instead of a mouse?

    • @mattwester9093
      @mattwester9093  4 года назад

      Hi Mikael, I have been noticing as well that the video is slightly outdated. I have tested the code, and it should still work fine. If you still have errors later, please post them so I can know how to help. I will be creating an updated version shortly, though if you want to know how to have a button be pressed, it would look something along the lines of this(this is not actual code, I typed it here in the comments, so you won't be able to copy and paste it. I just wrote it to let you see the process needed):
      float distance;
      void OnMouseOver()
      {
      distance = new Vector3.Distance(item.transform.position, tempParent.transform.position)
      if(distance

    • @Paaskedrengen
      @Paaskedrengen 4 года назад

      @@mattwester9093 Thanks for the reply. I done it like this:
      private void OnMouseOver()
      {
      if (distance

    • @Paaskedrengen
      @Paaskedrengen 4 года назад

      I tried again, now with a trigger collider:
      private void OnTriggerEnter(Collider other)
      {
      if (other.tag == "Player")
      {
      Debug.Log("player");
      if (distance

    • @mattwester9093
      @mattwester9093  4 года назад

      @@Paaskedrengen Are you setting your distance float? In the above code it is not set, and that could be the issue. Be sure to update it in the OnMouseOver method.

    • @Paaskedrengen
      @Paaskedrengen 4 года назад

      @@mattwester9093 Yes put the code: distance = new Vector3.Distance(item.transform.position, tempParent.transform.position) didnt work. There is nothing called Vector3.Distance

  • @stancollins1510
    @stancollins1510 5 лет назад

    can u add script to description?

  • @FweddyFwazbear
    @FweddyFwazbear 6 лет назад

    Awesome video, but can you give us the code? I have tried to put it out myself, but I have had issues. can you paste it into the description or reply it to me?

    • @fast07hzmc401
      @fast07hzmc401 3 года назад

      so what happen did you fix it ?

    • @FweddyFwazbear
      @FweddyFwazbear 3 года назад

      @@fast07hzmc401 I never figured it out

  • @grzesiufranek9296
    @grzesiufranek9296 5 лет назад +2

    i have error:
    error CS1513: } expected

    • @cgs7663
      @cgs7663 5 лет назад +5

      just put a } on that line

  • @theleeboys1098
    @theleeboys1098 4 года назад

    When I load the scene in game mode the object with this script just disappears, anyone know how to fix that?

  • @TommyworldABC
    @TommyworldABC 6 лет назад

    Hi Glad to see you Matt ! And Thanks for Upload this Pick Up Script. um.. I just have a Simple Question for You. I Written Your Code and Apply to Item. and I can see How to Pick Up the Item But If i Throw this Item it doesn't Throw to forward just Thrown back. I dont Know Why Am i Wrong With this Code Thank you For Read This. Have a good Day Matt. ^_^

    • @mattwester9093
      @mattwester9093  6 лет назад +1

      shiwook cho it sounds like you may have a different rotation than me on your character. Something simple you can do is replace one word in the code.
      In the section of the script where we handle throwing, instead of saying vector3.forward, put in vector3.back, and that should fix your problem! Happy coding.

    • @TommyworldABC
      @TommyworldABC 6 лет назад

      Oh My God... You Are Genius .. I dint think about that Vector3.back Wow It Works For me Thank You Matt. ~(^~^~