Raycast pickup and drag..how to??

Hi I’m new to unity 3d andnew to coding… I know a little of lua but now I’m trying to learn unity3d and c# and JS a little

so I’m trying to create my own personal raycast pickup and drag script to add to my character

after reading a bit of tutorial and watched some youtube videos i have this

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DragRigidbodyUp : MonoBehaviour {
   
    public GameObject playerCam;
   
    public string GrabButton = "Grab";
    public string ThrowButton = "Throw";
   
    public float PickupRange = 3f;
    public float ThrowStrength = 50f;
    public float distance = 3f;
    public float maxDistanceGrab = 4f;
   
    private Ray playerAim;
    private GameObject objectHeld;
    private bool isObjectHeld;
   
    void Start () {
        isObjectHeld = false;
        objectHeld = null;
    }
   
    void FixedUpdate () {
        if(Input.GetButton(GrabButton)){
            if(!isObjectHeld){
                tryPickObject();
            } else {
                holdObject();
            }
        }else if(isObjectHeld){
            DropObject();
        }
       
        if(Input.GetButton(ThrowButton) && isObjectHeld){
            isObjectHeld = false;
            objectHeld.GetComponent<Rigidbody>().useGravity = true;
            ThrowObject();
        }       
    }
   
    private void tryPickObject(){
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
        RaycastHit hit;
       
        if (Physics.Raycast (playerAim, out hit, PickupRange)){
            if(hit.collider.tag == "Pickupable"){
                isObjectHeld = true;
                objectHeld = hit.collider.gameObject;
                objectHeld.GetComponent<Rigidbody>().useGravity = false;
                objectHeld.GetComponent<Rigidbody>().freezeRotation = true;
            }
        }
    }
   
    private void holdObject(){
        Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
       
        Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
        Vector3 currPos = objectHeld.transform.position;
       
        objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;
       
        if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
        {
            DropObject();
        }
    }
   
    private void DropObject()
    {
        isObjectHeld = false;
        objectHeld.GetComponent<Rigidbody>().useGravity = true;
        objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
        objectHeld = null;
    }
   
    private void ThrowObject()
    {
        objectHeld.GetComponent<Rigidbody>().AddForce(playerCam.transform.forward * ThrowStrength);
        objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
        objectHeld = null;
    }
}

but when I click on the object nothing happen

can someone help me…

thanks in advance

Please put your code in codetags, see here: Using code tags properly - Unity Engine - Unity Discussions
That way it is barely readable :slight_smile:

ok …done …thanks

have you setup the input manager with “Grab” and “Throw” inputs?

edit:

do you mean “click” as in mouse? there is nothing about mouse input in that script…

Yep!!! you are my God!!! LOL

I didnt setup any button in the input manager, now created the Grab and Throw inputs and my script works

many thanks…

but I noticed that when I must throw my object I must continue pressed the right mouse button
what i have to do to throw my object simply pressing button with one click??

upload

you’re probably running into issues with using input during fixedUpdate, fixedUpdate doesn’t run every frame so it might miss some inputs (i.e. you have to hold the button until it detects it). Ideally you want to use input detection in Update (the one that does run every frame).

eheheh my knowledge in scripting is very scrace and I dont know how I can modifiy the script with “notes” you give to me…