Search Unity

[SOLVED]got drag item working But how do i lock the terrain?

Discussion in 'Scripting' started by t4d, Sep 14, 2009.

  1. t4d

    t4d

    Joined:
    Mar 19, 2009
    Posts:
    7
    Hi I got drag item working But how do i stop the terrain from being dragged too ?

    how do i lock it OR make the drag NOT touch the terrain ?

    here is the script I'm using to drag the items THANKS to the author found it on the forum somewhere

    pragma strict
    // Attach this script to an orthographic camera.
    private var object : Transform; // The object we will move.
    private var offSet : Vector3; // The object's position relative to the mouse position.

    function Update () {
    var ray = camera.ScreenPointToRay(Input.mousePosition); // Gets the mouse position in the form of a ray.
    if (Input.GetButtonDown("Fire1")) { // If we click the mouse...
    if (!object) { // And we are not currently moving an object...
    var hit : RaycastHit;
    if (Physics.Raycast(ray, hit, Mathf.Infinity)) { // Then see if an object is beneath us using raycasting.
    object = hit.transform; // If we hit an object then hold on to the object.
    offSet = object.position-ray.origin; // This is so when you click on an object its center does not align with mouse position.
    }
    }
    }
    else if (Input.GetButtonUp("Fire1")) {
    object = null; // Let go of the object.
    }
    if (object) {
    object.position = Vector3(ray.origin.x+offSet.x, object.position.y, ray.origin.z+offSet.z); // Only move the object on a 2D plane.
    }
    }
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You could set the layer of the terrain to Ignore Raycast. Alternatively, you can check the name of the transform that is hit by the raycast - if the name is that of your terrain object, then don't perform the drag.
     
  3. t4d

    t4d

    Joined:
    Mar 19, 2009
    Posts:
    7
    your a legend Andeeee :D

    Thanks