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 Guidez · Feb 17, 2013 at 04:08 AM · objectdrag

How drag a block along Z-Axis and keep it under the mouse?

With the below code, I am able to click and drag an object, restricting said objects transforms to only X and Z. The issue is that when you drag the cube towards the edges of the screen, the object will either fall behind or go ahead of the mouse pointer, dependent on what "factor" I set. Is there a better way to have drag the cube around?

This is for a SimCity'ish type game I am creating: I want to be able to drag an object around, as well as have the ability to freely rotate the camera around.

 var factor : float = 1.50;
  
 private var objectStartPos : Vector3;  // Initial position of object
 private var mousePosition : Vector3;   // Initial position of the mouse in world coordinates
 private var baseZDistance : float;         // Base Z distance to use in conversion
 
 
 function OnMouseDown(){
     objectStartPos = transform.localPosition;
     var worldScreenPoint : Vector3 = Camera.main.WorldToScreenPoint(objectStartPos);
     baseZDistance = worldScreenPoint.z;
     mousePosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, baseZDistance);
     mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
 }
  
 function OnMouseDrag(){
     worldScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, baseZDistance);
     worldScreenPoint = Camera.main.ScreenToWorldPoint(worldScreenPoint);
  
     var worldScreenPoint2 : Vector3 = objectStartPos;    
     worldScreenPoint2 = objectStartPos;
     worldScreenPoint2.x = Mathf.Round(worldScreenPoint2.x + (worldScreenPoint.x - mousePosition.x) / 2) * 2;
     worldScreenPoint2.z = Mathf.Round(worldScreenPoint2.z + (worldScreenPoint.z - mousePosition.z) / 2) * 2; 
  
     transform.localPosition = worldScreenPoint2; 
 }
Comment
Add comment · Show 2
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 · Feb 17, 2013 at 05:34 AM 0
Share

What is the point of view of the camera? What axis are you looking down and is the camera tilted?

avatar image AlucardJay · Feb 18, 2013 at 03:30 AM 0
Share

Guidez : I've decided to scrap the project. $$anonymous$$y current knowledge and understanding of coding and Unity is way too little for what's needed for this particular game, so I am going to start with something a LOT smaller. Thanks!

$$anonymous$$e : while it is probably a good idea do do alot of small simple projects first when starting out, don't scrap your project, just put it on the shelf for later =]

In regard to your problem, what you need to learn about is layers and Layer$$anonymous$$ask. With this, you can cast a ray on your terrain ignoring the 'cube', so the cube will stay under the mouse while you are dragging.

  • http://docs.unity3d.com/Documentation/Components/Layers.html

  • http://answers.unity3d.com/questions/8239/what-is-the-purpose-of-layers.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Layer$$anonymous$$ask.html

the layermask bitshift stuff can be very confusing. Another way is to use the Inspector. First create your new layer, assign it to your object, then in script create a variable for your Layer$$anonymous$$ask, eg :

  var terrainLayer : Layer$$anonymous$$ask;

In the Inspector, there should be a drop-down box with all the layers, just tick the layer you want to raycast against.

 if ( Physics.Raycast( rayPos, rayDir, rayHit, rayDist, terrainLayer ) )

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Guidez · Feb 18, 2013 at 03:07 AM

I've decided to scrap the project. My current knowledge and understanding of coding and Unity is way too little for what's needed for this particular game, so I am going to start with something a LOT smaller. Thanks!

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 AlucardJay · Feb 18, 2013 at 03:31 AM 0
Share

Noooooo.

BTW, Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

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

10 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

Related Questions

OnMouseUp() Click Display Effect. 1 Answer

Drag Camera Around an Object With Kinect (Official Plug-In) 0 Answers

Object moves to camera position when clicked with mouse 1 Answer

1 finger object dragging ignoring the second one 0 Answers

How do I apply drag rigidbody? 2 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