- Home /
movement control with keyboard & mouse
I am using a 2 scripts on my main camera to let the user move around my scene, but can't get them to work together:
This is the script for the mouse to zoom/orbit/pan:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var zoomRate = 2;
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;
}
var mover : Transform;
function LateUpdate () {
if (!target) {return;}
if (Input.GetMouseButton(2)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
}
distance += -Input.GetAxis("Mouse ScrollWheel") * zoomRate * Mathf.Abs(distance);
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 + mover.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);
}
which actually, works in tandem with an empty game object, called "Mover", that has this script:
function Update () {
if (Input.GetMouseButton(1)) {
transform.rotation = cam.rotation;
transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * moveSpeed);
transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * moveSpeed, Space.World);
}
but my question is, how do I get the above 2 scripts to work with the following script that uses the keyboard to move around the scene:
var speed = 5.0;
function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
Answer by AngryMarine · Aug 29, 2011 at 01:48 AM
I think I understand what you are asking. Your first script manipulates the camera while the second manipulates something called 'mover'. Your third script is attempting to manipulate the camera correct?
Your issue, from what I see, is that your third script is trying to manipulate the position of the camera via transform.Translate (which changes the object's position). Both scripts are fighting to change the same transform.
Here: "transform.Translate(x, 0, z);" is being update at UPDATE
"transform.position = position + mover.position;" is being updated every late update which happens AFTER update which over writes UPDATE's changes so you never get the changes done in UPDATE.
Here's my idea how to solve this. It can be done in your first script and eliminates the need for the third:
var scrollScreen : boolean = false;
// Pick a scripted modifier or keypress to switch scrollScreen between true and false
if(scrollScreen) {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
} else {transform.position = position + mover.position;}
Did this help at all? If so please close out this question by accepting the answer or commenting that it didn't not resolve it. Others may have a similar question and it helps to see an accepted answer.
It totally helped, thank you so much. And many apologies for my late reply. Yes, I ended up using the CapsLock key as the scrollScreen activator, but using Get$$anonymous$$eyDown (which has to be called in the Update function) so that the script looks like this:
var mover : Transform;
var scrollScreen :boolean = false;
function Update () {
if (!target) {return;}
var speed = 5.0;
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.CapsLock)){
scrollScreen = !scrollScreen ;
}
if(scrollScreen){
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
} else {
transform.rotation = rotation;
transform.position = position + mover.position;
}
}
Please note: The above is not the entire script, just the revised portion that is being discussed in this posting. The rest of the script (can post upon request) is tweaked to exclude from the LateUpdate function those commands which are now included in the Update function.
Answer by meena · Aug 31, 2011 at 01:15 PM
It totally helped, thank you so much. And many apologies for my late reply. Yes, I ended up using the CapsLock key as the scrollScreen activator, but using GetKeyDown (which has to be called in the Update function) so that the script looks like this:
var mover : Transform;
var scrollScreen :boolean = false;
function Update () {
if (!target) {return;}
var speed = 5.0;
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
if(Input.GetKeyDown(KeyCode.CapsLock)){
scrollScreen = !scrollScreen ;
}
if(scrollScreen){
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
} else {
transform.rotation = rotation;
transform.position = position + mover.position;
}
}
Please note: The above is not the entire script, just the revised portion that is being discussed in this posting. The rest of the script (can post upon request) is tweaked to exclude from the LateUpdate function those commands which are now included in the Update function.
Your answer
Follow this Question
Related Questions
OnGUI() and key pressing problems. 2 Answers
Disable Mouse click 1 Answer
Detecting which way my mouse is moving the most 1 Answer
moving an object with mouse ( my script is not smooth enough ) 0 Answers
Check Mouse Movement on GameObject 0 Answers