void OnMouseDrag() { if (Lerping) { return; } r = FPCam.ScreenPointToRay(Input.mousePosition); float entry; Vector3 newpos; if (_dragDirection == "xz") { DragPlane.Raycast(r, out entry); d = entry; newpos = r.GetPoint(d) - _hoverPos - _mouseOffset; if (Mathf.Abs(Input.mousePosition.x - Screen.width / 2) > Screen.width * 0.45f || Mathf.Abs(Input.mousePosition.y - Screen.height / 2) > Screen.height * 0.45f) { Vector2 Dir = (new Vector2(Input.mousePosition.x, Input.mousePosition.y) - CentreScreen).normalized; Vector3 FPCamPos = FPCam.transform.position; FPCam.transform.parent.Translate(new Vector3(Dir.x, Dir.y, 0), FPCam.transform); Vector3 TranslationIncrement = FPCam.transform.position - FPCamPos; LerpStartPt += TranslationIncrement; } } else { DragPlane.Raycast(r, out entry); d = entry; newpos = new Vector3(ObjStartPos.x, r.GetPoint(d).y - _hoverPos.y - _mouseOffset.y, ObjStartPos.z); } AttachedTransform.position = newpos; // -Vector3.up * _hoverHeight; }
void OnMouseDrag() { ray = FPCam.ScreenPointToRay(Input.mousePosition); float entry; DragPlane.Raycast(ray, out entry); d = entry; Vector3 _mouseDragVector = (ray.GetPoint(d) - _mouseStartPos) * _axisSign; if (_axis == "x") { float _mouseDragDist = Vector3.Dot(_mouseDragVector, transform.right); AttachedTransform.localScale = AttachedTransformStartScale + Vector3.right * _mouseDragDist * 0.1f; } if (_axis == "y") { float _mouseDragDist = Vector3.Dot(_mouseDragVector, transform.up); AttachedTransform.localScale = AttachedTransformStartScale + Vector3.up * _mouseDragDist * 0.1f; } if (_axis == "z") { float _mouseDragDist = Vector3.Dot(_mouseDragVector, transform.forward); AttachedTransform.localScale = AttachedTransformStartScale + Vector3.forward * _mouseDragDist * 0.1f; } }
void OnMouseDown() { FPCam.transform.parent.GetComponent <RoadBuilder>().Dragging = true; RaycastHit hit; ray = FPCam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (transform.InverseTransformPoint(hit.collider.bounds.center).x > 0.1) { _axis = "x"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).y > 0.1) { _axis = "y"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).z > 0.1) { _axis = "z"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).x < -0.1) { _axis = "x"; _axisSign = -1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).y < -0.1) { _axis = "y"; _axisSign = -1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).z < -0.1) { _axis = "z"; _axisSign = -1; } } Debug.Log(_axisSign.ToString() + _axis); FPCam.transform.parent.GetComponent <BuilderCamController>().FreezeTilt = true; GizmoStartPos = transform.position; GizmoStartScale = transform.localScale; AttachedTransformStartScale = AttachedTransform.localScale; ray = FPCam.ScreenPointToRay(Input.mousePosition); float entry; if (_axis == "x") { DragPlane = new Plane(transform.forward, transform.position); //dragplane is an xy plane } if (_axis == "y") { DragPlane = new Plane(transform.right, transform.position); //dragplane is an zy plane } if (_axis == "z") { DragPlane = new Plane(transform.up, transform.position); //dragplane is an xz plane } DragPlane.Raycast(ray, out entry); d = entry; _mouseStartPos = ray.GetPoint(d); }
void Update() { if (Lerping) { Vector3 NewPos = Vector3.Slerp(LerpStartPt, LerpEndPt, (Time.time - LerpStartTime)); FPCam.transform.position = NewPos; if (MouseDown) //we're moving towards the marker { //Dont look straight at the marker but lerp the camera towards it if (_dragDirection == "xz") //for xz we are looking down in it so we need to say which way is up { LerpEndRot = Quaternion.LookRotation(this.transform.position - FPCam.transform.position, this.transform.position - FPCam.transform.parent.position); } else { LerpEndRot = Quaternion.LookRotation(this.transform.position - FPCam.transform.position); } } else //we're returning towards the builder { LerpEndRot = LerpStartRot; } Quaternion NewRot = Quaternion.Slerp(FPCam.transform.rotation, LerpEndRot, (Time.time - LerpStartTime)); FPCam.transform.rotation = NewRot; if (Vector3.Distance(NewPos, LerpEndPt) < 0.02f) { Lerping = false; if (MouseDown) { RaycastHit hit; float entry; ray = FPCam.ScreenPointToRay(Input.mousePosition); Physics.Raycast(ray, out hit); if (_dragDirection == "xz") { //create a horizontal plane DragPlane = new Plane(Vector3.up, transform.position); } else { //create a plane that faces the camera Vector3 p = FPCam.transform.InverseTransformPoint(transform.position); _horiz = new Vector3(p.x, 0, p.z); _horiz.Normalize(); DragPlane = new Plane(_horiz, transform.position); } DragPlane.Raycast(ray, out entry); d = entry; _mouseOffset = ray.GetPoint(d) - transform.position; } else { FPCam.transform.parent.GetComponent <BuilderCamController>().FreezeTilt = false; } } } }
void OnMouseDown() { MouseDown = true; FPCam.transform.parent.GetComponent <RoadBuilder>().Dragging = true; RaycastHit hit; ray = FPCam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (transform.InverseTransformPoint(hit.collider.bounds.center).x > 0.1) { _axis = "x"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).y > 0.1) { _axis = "y"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).z > 0.1) { _axis = "z"; _axisSign = 1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).x < -0.1) { _axis = "x"; _axisSign = -1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).y < -0.1) { _axis = "y"; _axisSign = -1; } if (transform.InverseTransformPoint(hit.collider.bounds.center).z < -0.1) { _axis = "z"; _axisSign = -1; } } FPCam.transform.parent.GetComponent <BuilderCamController>().FreezeTilt = true; ObjStartPos = AttachedTransform.position; _startQuat = AttachedTransform.rotation; if (_axis == "x") { LerpEndPt = transform.position + transform.right * 20 * _axisSign; } if (_axis == "y") { LerpEndPt = transform.position + transform.up * 20 * _axisSign; } if (_axis == "z") { LerpEndPt = transform.position + transform.forward * 20 * _axisSign; } LerpStartRot = FPCam.transform.rotation; LerpStartPt = FPCam.transform.position; LerpStartTime = Time.time; Lerping = true; //transform.LookAt(FPCam.transform); }
void OnMouseDown() { float camdist = Vector3.Distance(FPCam.transform.parent.position, transform.position); MouseDown = true; FPCam.transform.parent.GetComponent <RoadBuilder>().Dragging = true; RaycastHit hit; ray = FPCam.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.collider.bounds.extents.y < 1) { _dragDirection = "xz"; } else { _dragDirection = "y"; } } FPCam.transform.parent.GetComponent <BuilderCamController>().FreezeTilt = true; ObjStartPos = AttachedTransform.position; if (_dragDirection == "xz") { LerpEndPt = AttachedTransform.position + _hoverPos + Vector3.up * camdist; } else { //WOrk out where to lerp the cam to Vector3 Offset = (FPCam.transform.position - transform.position).normalized; LerpEndPt = FPCam.transform.position; // transform.position + Offset * _hoverHeight*3; } LerpStartRot = FPCam.transform.rotation; LerpStartPt = FPCam.transform.position; LerpStartTime = Time.time; Lerping = true; Debug.Log("Lerp to " + LerpEndPt); AttachedObject.DisableClickColliders(); //transform.LookAt(FPCam.transform); }