Search Unity

Set distance to pick up objects?

Discussion in 'Physics' started by Dude_Mojo, Jul 15, 2016.

  1. Dude_Mojo

    Dude_Mojo

    Joined:
    Jul 14, 2016
    Posts:
    13
    Hello peeps im an avid unity noob. I followed a tutorial on the good ole youtube to write a script that would allow my player character to pick up objects with rigid bodies. But the script also gives me the ability to pick up objects at any distance. So ill be like 500 miles (km) from an object and pick it up and the object will then fly all the way over to me. So is it possible to set a distance at which I can pick up an object so that it can only be picked up from up close.

    Could i possibly make a "public float" that would allow me to adjust this distance?
    ( if that makes sense)

    here is my code:

    using UnityEngine;
    using System.Collections;

    public class PickupObject : MonoBehaviour {
    GameObject mainCamera;
    bool carrying;
    GameObject carriedObject;
    public float distance;
    public float smooth;
    // Use this for initialization
    void Start () {
    mainCamera = GameObject.FindWithTag ("MainCamera");
    }

    // Update is called once per frame
    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);
    o.transform.rotation = Quaternion.identity;
    }
    void pickup(){
    if (Input.GetKeyDown (KeyCode.E)) {
    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;
    p.gameObject.GetComponent<Rigidbody>().useGravity = false;
    }
    }
    }
    }

    void checkDrop() {
    if (Input.GetKeyDown (KeyCode.E)) {
    dropObject();
    }
    }

    void dropObject() {
    carrying = false;
    //carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
    carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
    carriedObject = null;
    }

    }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    If you look at the documentation for https://docs.unity3d.com/ScriptReference/Physics.Raycast.html you will find that it is possible to supply a maximum distance setting to the raycast. You can use this to solve your problem. Change your:
    Code (CSharp):
    1. if (Physics.Raycast (ray, out hit)) {
    to something like this:
    Code (CSharp):
    1. if (Physics.Raycast (ray, out hit, maxDistance)) {
    Where maxDistance is a float, which is set to the maximum distance at which you want the player to be able to pick up objects.

    BTW: Use the code tags when you post code!
     
  3. Dude_Mojo

    Dude_Mojo

    Joined:
    Jul 14, 2016
    Posts:
    13
    ok thanks PGJ will do