The following script gives me what I want in terms of camera control, except I want the user to have to click and drag for the camera to orbit, instead of having the camera orbit all the time depending on mouse position. What would be even nicer would be the mouse icon disappearing while the camera is being orbited via mouse/click/drag…
The code :
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var maxDist : float = 200;
var minDist : float = 30;
var zoomSpeed : float = 5;
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) {
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;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 distance < maxDist){
distance += zoomSpeed;
transform.Translate(Vector3.forward * -zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 distance > minDist){
distance -= zoomSpeed;
transform.Translate(Vector3.forward * zoomSpeed);
}
}
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);
}
I’m kindof new to unity, I’m a 3d modeler and would gladly accept any assistance over this matter.
Writing from an ipad so cannot give you code examples but hope this will help.
First create a bool called isMouseActive or something like that. Set it to false initially. Then wrap everything in your late update in it like
If(isMouseActive)
{
Do stuff
}
In your update function you then use input.getbuttondown and up.
I changed the late update function order, it doesn’t do much except giving me a tickbox in my mouse orbit script in the inspector. With that I can either have the mouse working as it were before or not at all…
The new code :
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var maxDist : float = 200;
var minDist : float = 30;
var zoomSpeed : float = 5;
private var x = 0.0;
private var y = 0.0;
var isMouseActive : boolean = false;
@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 Update () {
if(Input.GetButtonDown("Fire1"))
{
mouseIsActive = true;
}
if(Input.GetButtonUp("Fire1"))
{
mouseIsActive = false;
}
Screen.showCursor = false;
}
function LateUpdate () {
if(isMouseActive) {
if (target) {
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;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 distance < maxDist){
distance += zoomSpeed;
transform.Translate(Vector3.forward * -zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 distance > minDist){
distance -= zoomSpeed;
transform.Translate(Vector3.forward * zoomSpeed);
}
}
}
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);
}
Or am I targeting the wrong script for that, maybe it should be in my ThirdPersonController script …?
Okay, things weren’t in the right place apparently, now it rotates as expected and the cursor disappears and re-appears when desired. Only thing now is I can only move my character controller when the mouse button is down…
Code for mouse orbit is now as such :
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var maxDist : float = 200;
var minDist : float = 30;
var zoomSpeed : float = 5;
private var x = 0.0;
private var y = 0.0;
var isMouseActive : boolean = false;
@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 Update () {
if(Input.GetButtonDown("Fire1"))
{
isMouseActive = true;
Screen.showCursor = false;
}
if(Input.GetButtonUp("Fire1"))
{
isMouseActive = false;
Screen.showCursor = true;
}
}
function LateUpdate () {
if(isMouseActive) {
if (target) {
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;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 distance < maxDist){
distance += zoomSpeed;
transform.Translate(Vector3.forward * -zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 distance > minDist){
distance -= zoomSpeed;
transform.Translate(Vector3.forward * zoomSpeed);
}
}
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);
}
To move the camera, I’m using a Character Controller, a Character Motor, and a Plateform Input Controller. What and where should I change something to get my character controller move independently from my Fire1 mouse button being pressed…?
what seems to happen is that unity records the keyboard key being pressed, how long, and only updates the distance travelled when “Fire1” is pushed down.