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 Hims · Jul 09, 2012 at 11:13 AM · positiondrag and drop

raycast problem while drag and drop..

hello , i want to drag an object on another and i am using raycast for the it.. but following code is not working as programmed it do not print the log messages as one object collide with the object using raycast.. please help me to find the error..

 using UnityEngine;
 using System.Collections;
 
 public class Drag_drop : MonoBehaviour {
 
 private Vector3 screenPoint;
 private Vector3 offset;
  
  void OnMouseDown() 
  { 
  
  RaycastHit hit = new RaycastHit();
     Ray ray = new Ray();        
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
  if (Physics.Raycast (ray, out hit)) 
         {
             Debug.Log("pew pew");
             if (hit.collider.gameObject.name.Equals("Cube"))   
             {
                 Debug.Log("go brooks");
              //   BrooksCallOut.gameObject.SendMessage("gotoBrooks");
             }
         }
  
  //
  
  screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  }
  
  void OnMouseDrag() 
  { 
  Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
  Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  transform.position = curPosition;
  }
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kefrens · Jul 09, 2012 at 01:02 PM

Make sure that: 1. "Cube" has enabled collider. 2. "Cube" is not in "Ignore Raycast" layer.

btw. you don't need to initialize 'hit': RaycastHit hit = new RaycastHit(); because it will be overwritten by Physics.Raycast (ray, out hit) "out" is saying exactly this.

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 Hims · Jul 09, 2012 at 01:56 PM 0
Share

the raycast script is not working.. it is being executed when i click the sphere object.. ins$$anonymous$$d it should be executed when i drag and drop the sphere in front of the cube object..help.. what shoul be the reason..?

avatar image
0

Answer by kefrens · Jul 09, 2012 at 02:38 PM

According to documentation "OnMouseDown" is called only once when mouse is pressed. It is fine for start dragging the sphere, then if you want to drop "sphere" on "cube" You need to raycast for "cube" at onMouseUp also. But remember to turn off collisions on "sphere" if you plan to translate it with mouse, otherwise you will be always detecting sphere under mouse. Something like this pseudocode:

 bool is_drag = false;
 GameObject dragged_go;
 Vector3 drag_start;
 void OnMouseDown()
 {
    dragged_go = raycastSphere();
    if( null != dragged_go )
    {
 // start drag
       is_drag = true;
       dragged_go.collider.enable = false;
       drag_start = dragged_go.transform.position;
    }
 }
 
 void OnMouseDrag()
 {
    if( is_drag )
    {
       //... translate sphere to current mouse position
    }
 }
 
 void OnMouseUp()
 {
    if( is_drag )
    {
       // test if cube is under mouse by raycasting
       // if so then translate sphere to cube coordinates
       // else return sphere to it's original position
       dragged_go.transform.position = drag_start;
       // end drag
       is_drag = false;
       dragged_go.collider.enabled = true;
    }
 }

Have no time to write it fully, but you should get the idea.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera rotation around player while following. 6 Answers

Drag and Drop Problem 2 Answers

Drag mouse Up or Down to play or reverse animation 0 Answers

Setting the mouse position to specific coordinates 5 Answers

How to interpret Event.current.delta? 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