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;
}
`
`
This might have been asked before but how do I get the standard mouse orbit script? Thank you.
– AlvinHerawanFrom the Assets menu select Import Package/Scripts. It is one of the scripts that comes with the package.
– robertbuSeems like working solution, but can not make it work and I'm sure it something on me.
– StarHunter32Attach the script to the camera, select the camera, drag the object you want to orbit from the hierarchy onto the "target" variable in the MouseOrbit script in the Inspector window.
– robertbuTo start with, take my code and put it in its own script. and try it. It should work. I tested it before I posted as an answer to your question. What you have above is a mixture of the standard mouse orbit code and the code I gave you. You are using my code in Update(), but it is being completely overridden by the code in LateUpdate(). I'm not sure what changes you've made from the original MouseOrbit script. If you use the MouseOrbit script as it shipped with Unity, does it work for you? If not what does it not do?
– robertbu