Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by meena · Aug 28, 2011 at 02:57 PM · movementmousekeyboard

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryMarine · Aug 30, 2011 at 07:24 PM 0
Share

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.

avatar image meena · Aug 31, 2011 at 01:07 PM 0
Share

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.

avatar image AngryMarine · Aug 31, 2011 at 06:42 PM 0
Share

Great. Good luck with your game. Glad I could help.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges