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 Will Poole · Nov 17, 2010 at 05:03 PM · glitchmouse-dragmovinggameobject

Dragging object with a mouse from one point to another point, Have a big problem

Hi I have a massive problem with a piece of code that has the ability for the user to move an object by dragging it from a starting point to an end point.

The problem is that the code I have got has and does worked and I have implemented it into a different scene and done all the changes that are needed for it to work with the objects, but it won't work properly as the dragging has become very glitchy when trying to drag a object from the start point to the end point and back again. Plus the code is as it was when it previously worked.

If anyone who has a solution to this I would be very grateful for any help, as I have a deadline for Wednesday 24th November '10

Here is the code:

var useX : boolean; var useY : boolean; var useZ : boolean; private var mouseOn : boolean = false;

var start : Transform; var end : Transform;

function Update () {

 var mouseX : float = Input.GetAxis("Mouse X");

 if(useX && mouseOn){
     transform.position.x -= mouseX;
     transform.position.x = Mathf.Clamp(transform.position.x, start.position.x, end.position.x);

 }else if(useY && mouseOn){
     transform.position.y -= mouseX;
     transform.position.y = Mathf.Clamp(transform.position.y, start.position.y, end.position.y);


 }else if(useZ && mouseOn){
     transform.position.z -= mouseX;
     transform.position.z = Mathf.Clamp(transform.position.z,   start.position.z, end.position.z);       
 }


}

function OnMouseDown(){

 mouseOn = true; 

}

function OnMouseUp(){

 mouseOn=false;  

}

//Written by Will Goldstone 17/11/2010

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
1

Answer by skovacs1 · Nov 17, 2010 at 07:58 PM

You say glitchy, but you don't actually qualify what is being done incorrectly.

Here's a shorter version that does the same thing as your script:

var useX : boolean = true; var useY : boolean = true; var useZ : boolean = true;

var start : Transform; var end : Transform;

function OnMouseDrag() { if(!start || !end) return; var mouseX : float = Input.GetAxis("Mouse X"); if(useX) transform.position.x = Mathf.Clamp(transform.position.x+mouseX, start.position.x, end.position.x); else if(useY) transform.position.y = Mathf.Clamp(transform.position.y+mouseX, start.position.y, end.position.y); else if(useZ) transform.position.z = Mathf.Clamp(transform.position.z+mouseX, start.position.z, end.position.z); }

Note that your script as written will:

  • Only move on world axes. If you move your camera around, the direction will not be the same relative to the camera.
  • Move by the number of pixels the mouse moved world units. This will not mirror your mouse position. To get your movement, you would either need to convert the direction and amount into world units. or convert the object's position into pixels and back.
  • Only move on one axis at a time.
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 Will Poole · Nov 19, 2010 at 09:07 AM 0
Share

Thank you for that, it has seemed to corrected the problem to a degree

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

Why is my Gameovject not at 0 in the z acis? 2 Answers

Really weird Unity Camera Bug (In Editor) 2 Answers

Is it a problem or bug about reflection probes which causes glitch? 0 Answers

roll a ball pickups disappear on full screen 0 Answers

I see a sky through the building 1 Answer


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