Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 boby-bobs · Apr 12, 2015 at 09:38 PM · drag and dropdrag objects

Dragging a 3D object and limiting its Y position

I'm using this script that I found/modified to move a 3d object by clicking and dragging it. It works but I have one major flaw: I want to ensure that it never moves along the Y axis, only the X and Z. My game is an isometric 3D grid style game.

Here is my code. As you can see I will always set the Y to 2f as I don't want the concept of up/down, just left/right. The issue this creates is that after moving a few units, my mouse and the object I am dragging will no longer be together. I believe this is because my mouse is still moving in the Y plane. What I would like to resolve is that the cursor and game object being dragged remain in unison.

 using UnityEngine;
 using System.Collections;
 
 
 public class Drag : MonoBehaviour {
 
     private Vector3 screenPoint;
     private Vector3 offset;
 
     /**
      * Mouse is cilcked
      */
 
     void OnMouseDown() {
         Vector3 mouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(mouse);
     }
 
     /**
      * Mouse is dragged
      */
 
     void OnMouseDrag() {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         curPosition.y = 2f;
         transform.position = curPosition;
     }
 }

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 nicopavlotsky · Apr 12, 2015 at 09:42 PM 0
Share

You can freeze it's position from the inspector in the Y axys

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by maccabbe · Apr 12, 2015 at 09:53 PM

It seems like this problem would be solved by using a better method of getting the target position. Instead of using Camera.ScreenToWorldPoint you should instead use Camera.ScreenPointToRay to get a ray then find where the ray intersects with the plane y=2. For example

 void Update(){
    Plane plane=new Plane(Vector3.up,new Vector3(0, 2, 0));
    Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
    float distance;
    if(plane.Raycast(ray, out distance)) {
       transform.position=ray.GetPoint(distance);
    }
 }
Comment
Add comment · Show 3 · 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 boby-bobs · Apr 13, 2015 at 12:15 AM 0
Share

Incredible, thank you. I don't understand the Plane though.

  1. Create a plane, which acts like a divider/boundary I'm guessing

  2. Create a Ray, easy enough

  3. Float distance is = to nothing, not sure I understand

  4. Don't understand the if statement. What makes plane.Raycast true?

avatar image maccabbe · Apr 13, 2015 at 06:57 AM 0
Share

The Unity documentation does a good job explaining how Plane.Raycast work http://docs.unity3d.com/ScriptReference/Plane.Raycast.html

3) Plane.Raycast uses "out distance" as one of the arguments. It means that distance does not need to be defined before being passed and in this case is assigned the distance along the ray by Plane.Raycast. See the c# documentation for more details https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx.

4) Plane.Raycast is true if the ray intersects with the plane.

avatar image Fragmental · Jan 30, 2021 at 01:36 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DragObject : $$anonymous$$onoBehaviour
 {
     public float yOffsetForDraggedObject = 1;
 
     Plane plane;
     float distance;
     
     private void Start()
     {
        plane = new Plane(Vector3.up, new Vector3(0, yOffsetForDraggedObject, 0));
     }
 
     void On$$anonymous$$ouseDrag()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (plane.Raycast(ray, out distance))
         {
             transform.position = ray.GetPoint(distance);
         }
     }
 }

Here's essentially the same code, but the entire script, reformatted for efficiency. I changed Update() to On$$anonymous$$ouseDrag() to pick up the object when it's clicked.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Following instruction for drag and drop 3d object unity but not working 1 Answer

spawn and drag game object without leaving mouse button 1 Answer

Drag and drop picks an object too far away from mouse position 2 Answers

Collision while dragging in Canvas with IDragHandler 0 Answers

Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position 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