Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pick and drop object - collider

Discussion in 'Scripting' started by Tabacashi, Dec 19, 2014.

  1. Tabacashi

    Tabacashi

    Joined:
    Oct 3, 2014
    Posts:
    8
    Hello.

    Could anyone tell me how to collide picked object with terrain? I want to "block" camera when object would go away the map.
    How can i fix that?
    Its possible to throw picked object?

    <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();
    //rotateObject();
    } else {
    pickup();
    }
    }

    void rotateObject() {
    carriedObject.transform.Rotate(5,10,15);
    }

    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.GetKeyDown (KeyCode.E)) {
    int x = Screen.width / 2;
    int y = Screen.height / 2;

    Ray ray = mainCamera.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.rigidbody.isKinematic = true;

    }
    }
    }
    }

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

    void dropObject() {
    carrying = false;
    carriedObject.gameObject.rigidbody.isKinematic = false;
    carriedObject = null;
    }
    }
    </code>
     

    Attached Files:

  2. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    Ya look at for throwing the object you collected and destroyed.

    http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    also make a prefab of that object that your throwing.

    The block part I don't understand your question explain more detail if you want help. Thanks.

    Fyi please use tags make it easier to read your code. Check the sticky to know how.