I'm trying to use a ray to grab objects but, my script isn't working?
I have made a script and it should work, as there are no errors and to me it all makes sense but to no avail, the objects aren't being grabbed. My code (C#):
 using UnityEngine;
 using System.Collections;
 public class RayGrab : MonoBehaviour {
 GameObject mainCamera;
 bool carrying;
 GameObject carriedObject;
 public float distance;
 public float smooth;
 void Start () {
     mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
 }
 void Update (){
     if(carrying) {
         carry(carriedObject);
         checkDrop();
     }
     else {
         pickup();
     }
 }
 void carry(GameObject o) {
     o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
 }
 
 void pickup() {
     if(Input.GetButtonDown("Fire1")) {
         int x = Screen.width/2;
         int y = Screen.height/2;
         Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
         RaycastHit hit;
         if(Physics.Raycast(ray, out hit)) {
             Pickupable p = hit.collider.GetComponent<Pickupable>();
             if (p != null) {
                 carrying = true;
                 carriedObject = p.gameObject;
                 p.gameObject.GetComponent<Rigidbody>().isKinematic = true;
             }
         }
     }
 }
 void checkDrop() {
     if(Input.GetButtonDown("Fire1")) {
         dropObject();
     }
 }
 void dropObject() {
     carrying = false;
     carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
     carriedObject = null;
 }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Mario kart controller physics 0 Answers
Animation not working when Camera is attached to script 0 Answers
Vr hand rotation tracking,Vr hand tracking 0 Answers