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
1
Question by yyykobe · Jul 03, 2013 at 04:11 PM · positionmousemoveclickcoordinate

mouse clicking on the plane and drag to other position

Hello, this question is quite similar to the existed questions we can serach, I have searched for many related answers, but I am still stuck.

This question is something different from others.

I have a fixed plane, plane will never change its position,it look like a map. Also there is a camera in the top view, i want to click on the plane and drag to other position. In this drag process, i want the point i click on is follow the mouse move.

I want only the camera to move, not the plane.

ps: now, i already know how to get the coordinate of point which i touch on the screen by using following code

 cube=GameObject.Find("Cube");
 Vector3 startpos=new Vector3();
   if (Input.GetMouseButtonDown(0)){ // if click...
     // create a logical plane perpendicular to Y and at Y=0:
     Plane horPlane = new Plane(Vector3.up, Vector3.zero);
     // get the ray from the camera...
    
  Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
     print("mouseposition="+Input.mousePosition);
     startpos=Input.mousePosition;        
     float distance1 =0;
     // if the ray intersects the logical plane...
     if (horPlane.Raycast(ray, out distance1)){
       cube.transform.position = ray.GetPoint(distance1);
       print("cube position="+cube.transform.position);

the effect i want is look like a map. Thanks in advance!!

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
4

Answer by robertbu · Jul 04, 2013 at 07:01 AM

Using a mathematical plane is a good way to go if the camera is at an angle with respect to the plane. But it reading between the lines, I get the impression that you have a map on the XZ plane at the origin of 'Y' with a camera looking directly down. With that, you can simplify things a bit and use Camera.ScreenToWorldPoint(). Example:

 using UnityEngine;
 using System.Collections;
 
 public class DragMap : MonoBehaviour {
     private float dist;
     private Vector3 v3OrgMouse;
     
     void Start () {
         dist = transform.position.y;  // Distance camera is above map
     }
     
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             v3OrgMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
             v3OrgMouse = Camera.main.ScreenToWorldPoint (v3OrgMouse);
             v3OrgMouse.y = transform.position.y;
         } 
         else if (Input.GetMouseButton (0)) {
             var v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
             v3Pos = Camera.main.ScreenToWorldPoint (v3Pos);
             v3Pos.y = transform.position.y;
             transform.position -= (v3Pos - v3OrgMouse);
         }
     }
 }
Comment
Add comment · Show 6 · 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 yyykobe · Jul 04, 2013 at 08:04 AM 0
Share

Thanks for your answer, in fact the camera is not at the origin of 'Y' directly look down, it changes its position every moment, the direction it looks at has angle with the plane. In my function, i only need the camera move in a way(i can't imagine which path it should move).

I found the problem is that if i click the objectA which is exactly facing the camera lookat direcition, the drag on objectA is exactly what i want, however if i click the objectB which is besides objectA, the drag on objectB is not the right drag i expect(because objectB is not in the camera's lookat direction, B is the thing in the camera view).

Do you have some idea about this? Thanks.

avatar image yyykobe · Jul 04, 2013 at 08:12 AM 0
Share

It just like what you have said 'Using a mathematical plane is a good way to go if the camera is at an angle with respect to the plane.' If i understand the right meaning of your word, the camera is at an angle with respect to the plane.

avatar image robertbu · Jul 04, 2013 at 03:01 PM 1
Share

Here is another version using a mathematical plane. It assume the camera always stay at the same 'Y' position with respect to the plane:

 using UnityEngine;
 using System.Collections;
 
 public class Drag$$anonymous$$ap : $$anonymous$$onoBehaviour {
     private float dist;
     private Vector3 v3Org$$anonymous$$ouse;
     private Plane plane = new Plane(Vector3.up, Vector3.zero);
     
     void Start () {
         dist = transform.position.y;  // Distance camera is above map
     }
     
     void Update () {
         
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             float dist;
             plane.Raycast (ray, out dist);
             v3Org$$anonymous$$ouse = ray.GetPoint (dist);
             v3Org$$anonymous$$ouse.y = 0;
         } 
         else if (Input.Get$$anonymous$$ouseButton (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             float dist;
             plane.Raycast (ray, out dist);
             Vector3 v3Pos = ray.GetPoint (dist);
             v3Pos.y = 0;            
             transform.position -= (v3Pos - v3Org$$anonymous$$ouse);
         }
     }
 }
avatar image moghes · Jul 18, 2013 at 09:28 AM 0
Share

@yyykobe can you set robertbu's answer as true, just tried and having great results :)

avatar image sethillgard · Aug 03, 2014 at 08:16 AM 0
Share

Great answer!

Show more comments

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

16 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 avatar image

Related Questions

click on screen to get the coordinate on the ground(x,z plane) 1 Answer

Move object towards mouse but if mouse moves object fallows it's original path 0 Answers

Move to a clicked point? 1 Answer

Firing projectile towards mouse position from player... 0 Answers

Move on mouse click in Orthografic 30x45x0 angle 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