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 DerTeufel · Jan 27, 2015 at 05:20 AM · 3dobjectdragperspectivedrop

Drag and Drop with perspective camera 3D

Hello!

I want to drag and drop 3d objectives in 3D Space with a perspective camera (55° top down). Is there a chance to get the objective always in the same hight in 3d space over a terrain?

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
2

Answer by Simeon · Jan 27, 2015 at 09:09 AM

Just use a Plane to raycast with a ray from the camera, if you are doing an RTS. or you can always shoot a ray from the camera Camera.ScreenPointToRay and get a point with a desired length r.GetPoint

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 DerTeufel · Jan 28, 2015 at 02:45 PM 0
Share

Thank you! I know how it could work theoretical... Do you have any code examples?

I have a Terrain. On that terrain are some gameobjects with rigidbody on it. When I drag one of them with the mouse and move it arround, it should always have the same distance between it and the ground (uneven terrain). With those scripts out there (Unity Wiki e.g.), the y-position of the gameobject moves under the terrain for example. I want a fixed distance till drop...

When i use a camera with perfect 90° top down angle. It works. When i use 55° for my game, the objects move up on y position by moving mouse up and move down on y position by moving mouse down.

Example:

 using System.Collections;
 using UnityEngine;
  
 class DragTransform : $$anonymous$$onoBehaviour
 {
     
     private bool dragging = false;
     private float distance;
  
    
      
     void On$$anonymous$$ouseDown()
     {
         distance = Vector3.Distance(transform.position,    Camera.main.transform.position);
         dragging = true;
     }
  
     void On$$anonymous$$ouseUp()
     {
         dragging = false;
     }
  
     void Update()
     {
         if (dragging)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Vector3 rayPoint = ray.GetPoint(distance);
             transform.position = rayPoint;
         }
     }
 }
avatar image DerTeufel · Jan 28, 2015 at 04:22 PM 1
Share

ok, i solved the problem with the distance by asking for the distance in the Update function and not in the On$$anonymous$$ouseDown function:

 void Update()
     {
         if (dragging)
         {
             distance = Vector3.Distance(new Vector3(transform.position.x, transform.position.y +1f,transform.position.z),Camera.main.transform.position);
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Vector3 rayPoint = ray.GetPoint(distance);
             transform.position = rayPoint;
         }
     }



Now it works with the 55° angle... But there are some bugs on a navmesh baked on my terrain...

avatar image OfficeThrashing · Jan 01, 2019 at 12:06 PM 0
Share

Thanks This Link Works like charm...... :-)

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

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

Related Questions

Drag 3d objects on a 3d scenario? 1 Answer

Drag n Drop in a 3D orthographic environment 1 Answer

UI drag and drop 1 Answer

kite game should be 2D or3d?? 0 Answers

Simple drag and drop - no physics 4 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