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 darkhog · Jan 11, 2013 at 04:52 PM · c#mousedraggingmouse-drag

How to drag object in direction of camera?

I have this little script for dragging object in my level editor:

 using UnityEngine;
 using System.Collections;
 
 public class DragTile : MonoBehaviour {
 //declaring some variables
     private bool mousedwn;
     private Vector3 oldMousePosition;
     private float traveleddistX,traveleddistY = 0;
     void OnMouseDown() {
         if (!GameData.freemove){
             mousedwn = true;
         }
     }
     void OnMouseUp() {
         mousedwn = false;
     }
     void Start(){
         oldMousePosition = Input.mousePosition;
     }
     // Update is called once per frame
     void Update () {
         if (!GameData.freemove){
             if ((mousedwn) && (Input.GetMouseButton(0))){
                 if (Input.GetButton("Control")){
                     if ((this.oldMousePosition.x<Input.mousePosition.x) && (traveleddistX>=5)){
                         this.transform.Translate(0.25f,0,0);
                         traveleddistX = 0;
                     }
                     if ((this.oldMousePosition.x>Input.mousePosition.x) && (traveleddistX>=5)){
                         this.transform.Translate(-0.25f,0,0);
                         traveleddistX = 0;
                     }
                     if ((this.oldMousePosition.y<Input.mousePosition.y) && (traveleddistY>=5)){
                         this.transform.Translate(0,0,0.25f);
                         traveleddistY = 0;
                     }
                     if ((this.oldMousePosition.y>Input.mousePosition.y) && (traveleddistY>=5)){
                         this.transform.Translate(0,0,-0.25f);
                         traveleddistY = 0;
                     }
                 } else {
                     //this.transform.Translate(Input.GetAxis("Mouse X"),0,Input.GetAxis("Mouse Y"),Space.World);
                     if ((this.oldMousePosition.x<Input.mousePosition.x) && (traveleddistX>=5)){
                         this.transform.Translate(0.125f,0,0);
                         traveleddistX = 0;
                     }
                     if ((this.oldMousePosition.x>Input.mousePosition.x) && (traveleddistX>=5)){
                         this.transform.Translate(-0.125f,0,0);
                         traveleddistX = 0;
                     }
                     if ((this.oldMousePosition.y<Input.mousePosition.y) && (traveleddistY>=5)){
                         this.transform.Translate(0,0,0.125f);
                         traveleddistY = 0;
                     }
                     if ((this.oldMousePosition.y>Input.mousePosition.y) && (traveleddistY>=5)){
                         this.transform.Translate(0,0,-0.125f);
                         traveleddistY = 0;
                     }
                 }
         }
         
             traveleddistX += Mathf.Abs(this.oldMousePosition.x-Input.mousePosition.x);
             traveleddistY += Mathf.Abs(this.oldMousePosition.y-Input.mousePosition.y);
             oldMousePosition = Input.mousePosition;
             
         }
     }
 }

Problem is, that it doesn't take camera's orientation into consideration. This hurts, because when rotating camera by 180 degrees "tiles" are moving upwards when moving mouse downwards and when rotating camera by 90 deg., moving mouse upwards results in visible movement of left/right.

How to prevent that?

//edit: My editor features freemove camera (think of Minecraft's Creative mode or fly mod) and I want only move things in x/z plane.

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
Best Answer

Answer by darkhog · Jan 12, 2013 at 05:04 AM

Fixed with help of HPJ_ on IRC. Current code (if someone will have similar problem in future):

 using UnityEngine;
 using System.Collections;
 
 public class DragTile : MonoBehaviour {
 //declaring some variables
     private bool mousedwn;
     private Vector3 oldMousePosition;
     private float traveleddistX,traveleddistY = 0;
     void OnMouseDown() {
         if (!GameData.freemove){
             mousedwn = true;
         }
     }
     void OnMouseUp() {
         mousedwn = false;
     }
     void Start(){
         oldMousePosition = Input.mousePosition;
     }
     // Update is called once per frame
     void Update () {
         if (!GameData.freemove){
             if ((mousedwn) && (Input.GetMouseButton(0))){
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 
                 if (Input.GetButton("Control")){
                     float dist =0;
                     Plane WorldPlane = new Plane(new Vector3(0,1,0),new Vector3(0,0,0));
                     WorldPlane.Raycast(ray,out dist);
                     Vector3 TargetPosition = ray.GetPoint(dist);
                     //this.transform.position = Vector3.MoveTowards(this.transform.position, TargetPosition, 0.25f);
                     Vector3 TargetPosition2 = new Vector3 (
                         ( Mathf.RoundToInt ( TargetPosition.x * 4.0f ) ) / 4.0f ,
                         ( Mathf.RoundToInt ( TargetPosition.y * 4.0f ) ) / 4.0f ,
                         ( Mathf.RoundToInt ( TargetPosition.z * 4.0f ) ) / 4.0f );
                     this.transform.position = TargetPosition2;
                 } else {
                     float dist =0;
                     Plane WorldPlane = new Plane(new Vector3(0,1,0),new Vector3(0,0,0));
                     WorldPlane.Raycast(ray,out dist);
                     Vector3 TargetPosition = ray.GetPoint(dist);
                     Vector3 TargetPosition2 = new Vector3 (
                         ( Mathf.RoundToInt ( TargetPosition.x * 8.0f ) ) / 8.0f ,
                         ( Mathf.RoundToInt ( TargetPosition.y * 8.0f ) ) / 8.0f ,
                         ( Mathf.RoundToInt ( TargetPosition.z * 8.0f ) ) / 8.0f );
                     this.transform.position = TargetPosition2;
                 }
         }
         
             traveleddistX += Mathf.Abs(this.oldMousePosition.x-Input.mousePosition.x);
             traveleddistY += Mathf.Abs(this.oldMousePosition.y-Input.mousePosition.y);
             oldMousePosition = Input.mousePosition;
             
         }
     }
 }
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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Problem with dragging objects 1 Answer

Distribute terrain in zones 3 Answers

Trying to trigger multiple objects by holding down the mouse with OnMouseDown 1 Answer

Overlapping GUI Button priority 3 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