1
\$\begingroup\$

I'm trying to work with picking up and throwing objects. So far I've come up with a solution which does pick up the object but the same time disappears the object also. Please check to the functionality I've used.

public class pickupobject : MonoBehaviour {
     //GameObject mainCamera;
     bool carrying;
     GameObject carriedObject;
     Camera cam;
     public float distances;
     public float smooth;

     // Use this for initialization
     void Start()
     {
         cam = GameObject.Find("MainCamera").GetComponent<Camera>();
     }

     // Update is called once per frame
     void Update () {
         if(carrying)
         {
             carry(carriedObject);
         }
         else
         {
             pickup();
         }

     }

     private void pickup()
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             int x = Screen.width / 2;
             int y = Screen.height / 2;
             Ray ray = cam.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;
                 }
             }
         }
     }

     void carry(GameObject o)
     {
         o.GetComponent<Rigidbody>().isKinematic = true;
         o.transform.position = o.transform.position + cam.transform.position + cam.transform.forward * distances;
     }
 }
\$\endgroup\$
2
  • \$\begingroup\$ If the gameobject is active in scene then you are probably setting the wrong position in carry function. \$\endgroup\$ Commented Apr 24, 2017 at 12:20
  • \$\begingroup\$ When you have picked the object, check it's location in the Scene view to make sure you're not setting it's position to somewhere off-screen. \$\endgroup\$ Commented Apr 24, 2017 at 12:35

1 Answer 1

1
\$\begingroup\$

The problem here is, you're calling the function Carry in Update method, which means the position of your pickup object will keep increasing per frame by a factor of cam.transform.position + cam.transform.forward * distances , which will instantaneously shoot it off-screen.

If you just want to hold it, use the Carry function like this :

void carry(GameObject o)
     {
         o.GetComponent<Rigidbody>().isKinematic = true;
         o.transform.position = cam.transform.position + offset;
     }
\$\endgroup\$
4
  • \$\begingroup\$ What value should i use for offset? \$\endgroup\$ Commented Apr 24, 2017 at 15:11
  • \$\begingroup\$ Now i was trying to throw the carried object, but unable to throw it. I used this code but its recreating the same object in the same position void ThrowBall() { GameObject go = (GameObject)Instantiate(carriedObject, transform.position, Quaternion.identity); pickupable to = go.GetComponent<pickupable>(); to.Throw(carriedObject.transform.forward * speed); } Mentioned here, i used an emty gamobject and made the carried object as a child of that object. \$\endgroup\$ Commented Apr 24, 2017 at 16:17
  • \$\begingroup\$ It's because you're using Instantiate function which will create a new gameObject that you pass to it. And, you should ask a new question for this, add a link to this if you want. \$\endgroup\$ Commented Apr 25, 2017 at 5:14
  • \$\begingroup\$ Ive posted a new question. Please check to the link. gamedev.stackexchange.com/questions/140329/… \$\endgroup\$ Commented Apr 25, 2017 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .