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 Vedsten · Nov 14, 2010 at 10:42 PM · toolhand

Hand tool on a 2D plane

Hello, I'm trying to do a simple 2D "hand tool". I got it working but I accidentally deleted something I thought was obsolete. As it turns out, my first try was a stroke of luck, since I haven't been able to make it work again.

I experience a bug where the camera flickers when i try to drag. I'm sure I'm not the first person to do a drag script (maybe the biggest n00b tho), but i cannot find any other documentation on this. Any input would be appreciated.

//SCROLL var camCurrent : Vector3; //static camera position var pressedPosDif : Vector3; // difference between pressed and moved var pressedPos: Vector3; //starting coordinates for button hold var pressed:boolean = false; //is the button held?

//INITIATE function Start(){ camCurrent=camera.main.transform.position; }

//UPDATE

function Update () { if (Input.GetMouseButtonDown(0)) { pressedPos = (Camera.main.ScreenToWorldPoint(Input.mousePosition)); pressed = true; mouseCheck(); } }

function mouseCheck () { while (pressed) { pressedPosDif=pressedPos-(Camera.main.ScreenToWorldPoint(Input.mousePosition)); Camera.main.transform.position=camCurrent+pressedPosDif; yield; if (Input.GetMouseButtonUp(0)) { camCurrent=Camera.main.transform.position; pressed = false; } } }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skovacs1 · Nov 15, 2010 at 03:37 PM

This seems like more work than you need.

Why not do something like:

MouseTranslate.js (Attached to the camera)

var scale : float = 0.2f
function Update() {
    if(Input.GetMouseButton(0)) {
        transform.position -= transform.right * Input.GetAxis("Mouse X") * scale;
        transform.position -= transform.up * Input.GetAxis("Mouse Y") * scale;
    }
}

For more precise movement, you could do some screen point converson:

MouseDragPosition.js (Attached to the camera)

var planeDistance : float = 10.0f;
private var dragPoint : Vector3 = Vector3.zero;
function Update() {
    var screenPoint : Vector3 = camera.ScreenToWorldPoint(Vector3(
                                    Input.mousePosition.x,
                                    Input.mousePosition.y,
                                    planeDistance));
    if(Input.GetMouseButtonDown(0)) dragPoint = screenPoint;
    else if(Input.GetMouseButton(0)) transform.position += dragPoint-screenPoint;
}
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 Vedsten · Nov 18, 2010 at 11:43 PM 0
Share

Hey thanks, for the answer, wasn't quite what I was looking for since it creates a "sliding" effect unlike the standard hand tool. Thanks for the effort though

avatar image skovacs1 · Nov 19, 2010 at 09:15 PM 0
Share

You're concerned about 1:1-ish movement then? The Unity editor's hand tool is also subject to "sliding" as you call it, the further you move away. To mimic Unity's editor's hand tool more closely, you would simply a) subtract the movement ins$$anonymous$$d of add and multiply the movement by some factor which you can play with as you like. I find 0.2f works nicely for objects 5 units in front of the camera. If you want/need more precise movement, you could do some calculation with the world point of the mouse position. Code to come.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Trading card game. Need help scripting the cards. 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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