Pick up/throw object?

Hey guys, another really newbie question coming at you, I’m doing my best to figure C# and Unity out on my own but I’ve been grappling with this for too long now, hopefully someone can help me.

Quite simply, I’m using the camera to represent a character/player (not the best system, I know, but it works for the movement style needed so I’d prefer to keep this if possible), and what I really need is to be able to pick up a simple sphere when the mouse is clicked, then throw/drop the sphere when clicked again.

This probably sounds scoff-ably simple to many of you but please do bear in mind I started with C# and Unity 5 only a few days ago, so any help putting this basic script together would be much appreciated. I’ve scoured the internet but I can’t seem to find anything that fits what I need without having to completely rebuild my scene from scratch.

Considering I’d like the ball to be in front of the camera when it’s picked up, I have a feeling I’ll need to be using the transform.setparent function or similar, but I could really do with the help kickstarting the script.

Thanks!

well is simple really not that hard, but first I’ll recommend you to learn C# at least the basic then start in game dev. Now this is what you can do.

Edit: Well I made some changes,

1- On our camera object we will create a children empty object that will be our guide and lets call it Guide just like the image below:
81857-screenshot-20161109133016.png

-Re-position our guide object wherever you want.

2- We add the HoldItems.cs Script to our Camera Object and set all settings as the image below:
81859-screenshot-20161109133047.png

-You can edit the speed as want.

Set the Camera Collider to trigger.

Ball Collider doesn’t have to be trigger.

Now that’s all here the Script:

using UnityEngine;
using System.Collections;

public class HoldItems : MonoBehaviour {


    public float speed = 10;
    public bool canHold = true;
    public GameObject ball;
    public Transform guide;

   void Update()
  {
      if (Input.GetMouseButtonDown(0))
      {
          if (!canHold)
              throw_drop();
          else
              Pickup();
      }//mause If

      if (!canHold && ball)
          ball.transform.position = guide.position;
      
  }//update


    //We can use trigger or Collision
    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "ball")
            if (!ball) // if we don't have anything holding
                ball = col.gameObject;
    }

    //We can use trigger or Collision
    void OnTriggerExit(Collider col)
    {
        if (col.gameObject.tag == "ball")
        {
            if (canHold)
                ball = null;
        }
    }


    private void Pickup()
    {
        if (!ball)
            return;

        //We set the object parent to our guide empty object.
        ball.transform.SetParent(guide);

        //Set gravity to false while holding it
        ball.GetComponent<Rigidbody>().useGravity = false;

        //we apply the same rotation our main object (Camera) has.
        ball.transform.localRotation = transform.rotation;
        //We re-position the ball on our guide object 
        ball.transform.position = guide.position;

        canHold = false;
    }

    private void throw_drop()
    {
        if (!ball)
            return;

        //Set our Gravity to true again.
        ball.GetComponent<Rigidbody>().useGravity = true;

         // we don't have anything to do with our ball field anymore
         ball = null; 

        //Apply velocity on throwing
        guide.GetChild(0).gameObject.GetComponent<Rigidbody>().velocity = transform.forward * speed;

        //Unparent our ball
        guide.GetChild(0).parent = null;

        canHold = true;
    }
}//class

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectReplyIdAndLock, ObjectGrabIdAndMove

ObjectGrabIdAndLock goes on Main player,… ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers
Ryan kappes[105508-objectgrabscripts.zip|105508]
Exvalid@gmail.com

Check asset called Throw Object 3D.


Ok i have an issue where no matter where the character is the item i am picking up is just auto getting picked up and i’m trying to make something to pick up a rock and throw it at a security camera any fixes available for what i am trying to do Thanks!,Ok i have an issue where, wherever the rock i am trying to pick up is, i just left click and it picks up the rock no matter where the character is. is there a fix to this? i did everything in the boxes above and no fix is happening. Thanks!