void Start() { currentCamera = Camera.main; oldCameraPosition = currentCamera.transform.position; mask = 1 << LayerMask.NameToLayer("is Ground"); //Every screen dependent variable has to be scaled to fit any resolution initialDropDistance = PersonalMath.ScreenSizeCompensation(initialDropDistance); mouseVectorMultiplier = PersonalMath.ScreenSizeCompensation(mouseVectorMultiplier); }
// Use this for initialization void Start() { //Find the Objects. player = GameObject.Find("Player"); dummy = GameObject.Find("CameraDummy"); currentCamera = Camera.main; //Convert screen dependent values, to fitting values for the current game screen. thresholdWidth = PersonalMath.ScreenSizeCompensation(thresholdWidth); thresholdHeight = PersonalMath.ScreenSizeCompensation(thresholdHeight); }
// Update is called once per frame void Update() { if (buttonReleased && Input.GetMouseButton(0) && (!isDragging || Vector3.Distance(pickUpScreenPos, Input.mousePosition) <= DropDistance)) { //start dragging if (!isDragging) { //initiate dragging draggingObject = GetObjectFromMouseRaycast(); if (draggingObject) { draggingObject.GetComponent <Rigidbody>().gameObject.layer = LayerMask.NameToLayer("Ignore Raycast"); isDragging = true; pickUpScreenPos = currentCamera.WorldToScreenPoint(draggingObject.GetComponent <Rigidbody>().position); } } //while dragging else if (draggingObject != null) { DrObj = draggingObject.GetComponent <Rigidbody>(); pickUpScreenPos = currentCamera.WorldToScreenPoint(DrObj.position); MouseVector = PersonalMath.CalculateMouse3DVector(currentCamera, mask, pickUpHeight + heightOffset); //Apply force if (draggingDrag) { DrObj.AddForce((MouseVector - DrObj.transform.position).normalized * forceStrenght, ForceMode.Force); DrObj.drag = (currentWeightInfluence * 1) / Vector3.Distance(DrObj.transform.position, MouseVector); } else { DrObj.velocity = pickUpSpeed * ((MouseVector - DrObj.transform.position).normalized) * Vector3.Distance(MouseVector, DrObj.transform.position); } //Update Position for camera movement cameraDifference = currentCamera.transform.position - oldCameraPosition; DrObj.transform.position += cameraDifference; } } //stop dragging else { if (draggingObject != null) { DrObj = draggingObject.GetComponent <Rigidbody>(); DrObj.gameObject.layer = LayerMask.NameToLayer("Default"); DrObj.constraints = RigidbodyConstraints.None; DrObj.drag = 0; if (DrObj.velocity.y > upwardVelocityThreshhold && !draggingDrag) { DrObj.velocity = new Vector3(mouseVectorMultiplier * Input.GetAxis("Mouse X"), 0, mouseVectorMultiplier * Input.GetAxis("Mouse Y")); } draggingObject = null; } isDragging = false; } //Check if the mouse button was released if (Input.GetMouseButton(0) && !isDragging) { buttonReleased = false; } else { buttonReleased = true; } oldCameraPosition = currentCamera.transform.position; }