- Home /
Make a object move with middle mouse button
I'm trying to do a Camera movement like sketchfab. I've done the orbit and zoom script, but I can not make the pan. The target of my Orbit script is a 0,0,0 GameObject. I need to do this object move when i press the middlemouse button. how? I managed to do this script almost working. I press the middle button and move the object but he only move when i gave a click later.
My Pan Script:
public var mouseSensitivity : float = 1.0;
private var lastPosition : Vector3;
function Update()
{
if (Input.GetMouseButtonDown(2))
{
lastPosition = Input.mousePosition;
}
if (Input.GetMouseButton(2))
{
var delta : Vector3 = Input.mousePosition - lastPosition;
transform.Translate(delta.x * mouseSensitivity, delta.y * mouseSensitivity, 0);
lastPosition = Input.mousePosition;
}
}
UPDATE: I noticed that the script of the orbit movement is looking the target only when the mouse is clicked. Here's the script:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceMin = 3;
var distanceMax = 15;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (!Input.GetMouseButton(0))
{
return;
}
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
var hit : RaycastHit;
if (Physics.Linecast (target.position, transform.position, hit)) {
distance -= hit.distance;
}
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
How can i solve this?
Comment
Your answer
Follow this Question
Related Questions
Orbit/Pan/Zoom target problem. 2 Answers
Limiting Camera Movement 1 Answer
Pan function is not working in this script 0 Answers
Rotate, pan and zoom camera lerp touch 0 Answers
ORBIT CAMERA ZOOM LIMIT 0 Answers