BlueBear icon

DragAndDropbehaviour.cs

BlueBear | PRO | 10/28/13 10:12:45 AM UTC | 0 ⭐ | 383 👁️ | Never ⏰ | []
C# |

2.88 KB

|

None

|

0 👍

/

0 👎

using UnityEngine;
using System.Collections;
 
public class DragAndDropBehaviour : MonoBehaviour {
 
    private bool _mouseState;
    private GameObject target;
    private GameObject lastTarget;
    private Vector3 lastPos;
    
    public Vector3 screenSpace;
    public Vector3 offset;
    public float level = 0;
    
    // Update is called once per frame
    public void Update ()
    {
        #region MouseDown
        if (Input.GetMouseButtonDown (1)) 
        {
            RaycastHit hitInfo;
            target = GetClickedObject (out hitInfo);
            Debug.Log (target);
            if( target == null)
            {
                return;//target = null;
            }
            
            if (target != null && target.GetComponent<DraggableObject>().isDraggable == true) 
            {
                /*if(target.GetComponent<Pontoon>() != null)
                {
                    target.GetComponent<Pontoon>().deadlock = false;
                }*/
                _mouseState = true;
                screenSpace = Camera.main.WorldToScreenPoint (target.transform.position);
                offset = (target.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z))) * Time.deltaTime * 10;
            }
        }
        #endregion
        
        #region MouseUp
        if (Input.GetMouseButtonUp (1)) {
            /*if(target.GetComponent<Pontoon>() != null)
            {
                target.GetComponent<Pontoon>().deadlock = true;
            }*/
            _mouseState = false;
            level = 0;
        }
        #endregion
        
        #region Movement And Boundary Setting
        if (_mouseState) {
            if(target.transform.GetComponent<PlaceableObject>())
            {
                level = target.transform.GetComponent<PlaceableObject>().level;
                Debug.Log ("A New Level: " + level);
            }
            //keep track of the mouse position
            var curScreenSpace = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
 
            //convert the screen mouse position to world point and adjust with offset
            var curPosition = Camera.main.ScreenToWorldPoint (curScreenSpace) + offset;
            
            //setting boundaries
            if(curPosition.x < 1)
            {
                curPosition.x = 1;
            }
            if(curPosition.z < 1)
            {
                curPosition.z = 1;
            }
            if(curPosition.x > 59)
            {
                curPosition.x = 59;
            }
            if(curPosition.z > 59)
            {
                curPosition.z = 59;
            }
            
            //update the position of the object in the world
            curScreenSpace = new Vector3(Mathf.Round(curPosition.x), 0F + this.level, Mathf.Round(curPosition.z));
            target.transform.position = curScreenSpace;
        }
        #endregion
    }
 
    public GameObject GetClickedObject(out RaycastHit hit)
    {
        GameObject target = null;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
 
            target = hit.collider.gameObject;
        }
        return target;
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  • Syutrsz icon
    03/31/26 10:26:32 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎