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 navadeep2011 · May 14, 2013 at 07:25 AM · dragmousedrag

How will i drag my object only x and y axis?

Hi All,

I am new to unity 3d. I have a stop watch.

My task is i want to drag my stop watch in the hole scene . I wrote the code for x,y axis moving of the stop watch. but that is not moving.

My code is:

 using UnityEngine;
 using System.Collections;

 public class Moused : MonoBehaviour {
 public float sensitivity = 0.02f;
 private Vector3 v3Prev;
  
 void OnMouseDrag () {
 if (Input.GetMouseButtonDown(0)) {
 v3Prev = new Vector3(Input.mousePosition.x,Input.mousePosition.y,0);
 }
 if (Input.GetMouseButton (0)) {
             Vector3 newPos = transform.position;
 newPos.x = newPos.x + (Input.mousePosition.x - v3Prev.x) * sensitivity;
 newPos.y = newPos.y + (Input.mousePosition.y - v3Prev.y) * sensitivity;        
 transform.position = newPos;
 v3Prev = newPos;
 }
 }
 }

I don't know the problem for moving. i added box collider to the stop watch.

can i get a solution. or any help

Thanksℜgards

Shankar

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 robertbu · May 14, 2013 at 07:46 AM 0
Share

There are four coordinate systems in Unity: world, viewport, GUI, and screen. Input.mousePosition is in Screen coordinates. Your game object is in world coordinate. One way to convert between the two is Camera.ScreenToWorldPoint(). Note the 'Z' parameter of this function is the distance in front of the camera. There are lots of posts on drag and drop in UA.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by cwkx · May 16, 2013 at 09:44 AM

As robertbu said, Input.mousePosition is in "screen space" but you want to drag in "world space". Therefore:

 using UnityEngine;
 using System.Collections;
 
 public class DragScript : MonoBehaviour {
     
     private Vector3 dragPosition;
     
     void OnMouseDrag()
     {
         dragPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
         dragPosition = Camera.main.ScreenToWorldPoint(dragPosition);
         dragPosition.z = 0.0f; // assuming 2D game
     }
     
     void Update() {
         transform.position = dragPosition;
     }
 }
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
avatar image
0

Answer by SubatomicHero · May 14, 2013 at 07:47 AM

I think the order of your code is illogical. Think about what you need to do step by step. However as you're new to Unity I think the function should look like this:

 void OnMouseDrag()
 {
     if (Input.GetMouseButtonDown(0))
     {
         // you have a vector 3 position
         v3Prev = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
     }
 
 void Update()
 {
     // Do your gameobject movements here
     transform.position = v3Prev;
 }

Assuming this script is attached to your object of course. This is untested as I am at work. Hope this helps

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 navadeep2011 · May 14, 2013 at 07:52 AM 0
Share

This.. script didn't work for me...

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How will i drag my object with out object disturb? 0 Answers

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

Coding A Tornado Particle Effect 0 Answers

I need help with scripting 0 Answers

How to stretch my scale in unity 3d ? 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