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
2
Question by Josh 7 · Aug 27, 2010 at 10:29 PM · camerajavascriptmouseclickpan

Left Click and Pan

I want to write a script that allows the player to hold left click and pan the camera.

It should looks something like this:

OnMouseDown if mouse moves left camera pans to the right (locally I think)

Also middle mouse wheel to zoom.

And middle mouse button to rotate.

How would I go about accomplishing this in JavaScript?

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
3

Answer by tylo · Aug 28, 2010 at 01:01 AM

To answer your first question, I would store the coordinates of where your mouse first "clicked". Then, make a variable to determine that you are now dragging with the following psuedo-code.


Note: This is not a copy/paste affair, I'm just explaining my thought process.


var dragging : boolean; var mouseDownPos : Vector3;

if(OnMouseDown){ dragging = true; mouseDownPos = Mouse's position when you clicked; }

Now that you're dragging, continually grab the current position of the mouse.

var mouseDragPos : Vector3;
if(dragging)
    mouseDragPos = Mouse's position as you are dragging;

Then, subtract the two and move the camera's transform accordingly.

camera.Main = camera.Main.transform.position + ( mouseDownPos - mouseDragPos );
Comment
Add comment · Show 2 · 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 · Aug 28, 2010 at 03:29 AM 0
Share

thanks a lot you have invigorated my logic, and helped me answer all of my questions. I realized zoo$$anonymous$$g with the mouse wheel is just moving the camera locally on the x axis. And of course my middle mouse click is pretty much what you showed me with the panning.

avatar image tylo · Aug 28, 2010 at 04:34 AM 0
Share

Huzzah!!!!!!!!!

avatar image
0

Answer by jgstewart · Mar 08, 2012 at 08:35 AM

This assumes the script is running on the camera...

// ***** PANNING ***** // Hold Right Mouse Button to pan the screen in a direction if(Input.GetButtonDown("MouseRight")) { print ("Panning..."); viewScreenPanning = true; viewScreenOldMouseX = Input.mousePosition.x; viewScreenOldMouseY = Input.mousePosition.y; }

// If panning, find the angle to pan based on camera angle not screen if(viewScreenPanning==true) { if(Input.GetButtonUp("MouseRight")) { viewScreenPanning = false; } viewScreenPanVector = transform.TransformPoint(Vector3(-(viewScreenOldMouseX - Input.mousePosition.x) / viewScreenPanSpeed, 0.0, -(viewScreenOldMouseY - Input.mousePosition.y) / viewScreenPanSpeed)); // since we use a quick and dirty transform, reset the camera height to what it was viewScreenPanVector.y = viewScreenZoomHeight; transform.position = viewScreenPanVector; }

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

1 Person is following this question.

avatar image

Related Questions

Pan Camera with mouse? 1 Answer

Limiting Camera Movement 1 Answer

How To Detect MouseClick Using Raycast Java Script 0 Answers

How to access the light component on a camera object? 1 Answer

Sketchup-style camera movement/rotation?. 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