Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Josh 7 · Sep 03, 2010 at 03:50 AM · camerajavascriptrotatezoompan

Limiting Camera Movement

I have my script written out for my camera movement. I have "Pan" "Rotate" and "Zoom" functionality.

The problem is the rotation is really awkward. I need the rotation to be similar to an FPS. Zooming should only zoom so far or so close.

My questions are how can I refine my camera rotation and limit how close/far the camera can zoom?

Here is my script:

// This script controls camera movement. //Variables var horizontalSpeed = 0.5; var verticalSpeed = 0.5; var turnSpeed = 2;

function Update () { //"Pan Camera" if(Input.GetMouseButton(0)) { var h = horizontalSpeed -(Input.GetAxis ("Mouse X")); var v = verticalSpeed -(Input.GetAxis ("Mouse Y")); transform.Translate (h, v, 0); } //"Rotate Camera" else if(Input.GetMouseButton(1)) { h = turnSpeed Input.GetAxis ("Mouse X"); v = turnSpeed Input.GetAxis ("Mouse Y"); transform.Rotate (v, h, 0); } //"Zoom Camera Out" else if(Input.GetAxis("Mouse ScrollWheel") < 0) { var d = Input.GetAxis ("Mouse ScrollWheel") 20; transform.Translate (0, 0, d); } //"Zoom Camera In" else if(Input.GetAxis("Mouse ScrollWheel") > 0) { d = Input.GetAxis ("Mouse ScrollWheel") 20; transform.Translate (0, 0, d); } else { } }

I'm guessing that my only problem is limiting how far the camera can zoom/rotate. Any help is appreciated.

Comment
Add comment · Show 1
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 · Sep 03, 2010 at 04:20 AM 0
Share

I'm assu$$anonymous$$g that the camera script is attached to some sort of player object, and the zoom in/out is setting the relative position of the camera to that object..?

1 Reply

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

Answer by · Sep 03, 2010 at 04:18 AM

You could Mathf.Clamp to limit the values to a range, or just ifcheck before applying change. Personally, I prefer clamping to be sure of the value range, but your setup would probably work better with ifchecks.

As an example:

else if(Input.GetAxis("Mouse ScrollWheel") != 0)
{
   if ( transform.position.z >= -20 && transform.position.z <= 0 )
   {
      var d = Input.GetAxis ("Mouse ScrollWheel") * 20;
      transform.Translate (0, 0, d);
   }
}

Note that I combined both the zoom in and out into a single ifcheck, as the contents were identical. I then use an ifcheck to make sure that the 'z' position is between -20 and 0 (purely as examples, adapt to your own needs) before allowing the input to translate the position.

I'd also suggest that you changed the elseifs to 'if', otherwise you won't be able to zoom while either mouse button is held (unless that's intended functionality).

Regarding the camera rotation, the MouseLook script (from the standard assets) has a nice rotation setup. Might be worth taking a look at it, and seeing if it's similar to what you're after.

Comment
Add comment · Show 1 · 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 Josh 7 · Sep 03, 2010 at 01:26 PM 0
Share

unfortunately the mouselook script is in C# and I really wanted everything to be in JavaScript, but I suppose it shouldn't be to hard to convert. I'll give it a shot.

Thanks a lot for all your help.

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

No one has followed this question yet.

Related Questions

Camera scripting references 0 Answers

Zoom in on object and make other objects disappear on zoom?,Zoom in on object and make other objects disappear 0 Answers

Pan Camera with mouse? 1 Answer

I want to select an object and have the camera zoom in but inactive the other objects. I am having problems 1 Answer

Left Click and Pan 2 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