- Home /
Camera rotation on mouse drag?
Hello,
I'm trying to make an auto-orbit camera around an object.
What I couldn't find and figure out so far is the following: How to make the rotation be both directions depending on the dragging on the screen - when click and drag left to right the speed value to be positive (clockwise); when I click and drag right to left the value to be negative (anti-clockwise);.
Thanks a lot in advance!
var target : Transform; var distance = 2.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = 0.0; var yMaxLimit = 80;
var Speed : float = 0.14;
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 (target && Input.GetMouseButton(0)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
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); }
function Update () {
transform.LookAt(target);
transform.position += transform.right Speed Time.deltaTime;
}
Answer by robertbu · Mar 18, 2013 at 12:49 AM
You know there is a standard mouse orbit script? But to answer your question:
var speed : float = 5.0;
var target : Transform;
function Update () {
if (Input.GetMouseButton(0)) {
transform.LookAt(target);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X")*speed);
}
}
This might have been asked before but how do I get the standard mouse orbit script? Thank you.
From the Assets menu select Import Package/Scripts. It is one of the scripts that comes with the package.
Seems like working solution, but can not make it work and I'm sure it something on me.
Attach the script to the camera, select the camera, drag the object you want to orbit from the hierarchy onto the "target" variable in the $$anonymous$$ouseOrbit script in the Inspector window.
Yes, I have an empty game object. What I can not do is make the code you kindly provided to work. The rotation is always still one way only. Should I remove or add something I'm missing here.
var target : Transform;
var distance = 2.0;
var xSpeed = 180.0;
var ySpeed = 90.0;
var y$$anonymous$$inLimit = 0.0;
var y$$anonymous$$axLimit = 80;
var speed : float = 0.14;
private var x = 0.0;
private var y = 0.0;
@script AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target && Input.Get$$anonymous$$ouseButton(0)) {
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
}
function Update () {
transform.LookAt(target);
transform.position -= transform.right * speed * Time.deltaTime;
if (Input.Get$$anonymous$$ouseButton(0)) {
transform.LookAt(target);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("$$anonymous$$ouse X")*speed);
}
}
Answer by AlvinHerawan · Mar 19, 2013 at 02:44 AM
So far I've been exploring about camera scripting on the Asset Store. One solution I found was from the Camera Orbit Tool where you parent your camera to an empty game object and rotate that game object instead of the camera. This will give a distance between the camera and the object that the camera is orbiting.
Your answer
Follow this Question
Related Questions
The solution with camera rotation which is used in most strategic games 1 Answer
Mouse Orbit snapping issues 0 Answers
Mouse/Wacom stylus drag not working. 1 Answer
How to Rotate camera around object SMOOTHLY when using Input.GetKeyDown 1 Answer
Quaternion - Euler Angles, weird variable swapping when converting 2 Answers