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 easilyBaffled · May 21, 2012 at 05:25 PM · raycastmousepositionclick and draglevel-editor

Click and Drag not Clicking or Dragging

I am trying to create a level builder. My first task is to make a click and drag feature, tagged on to that is snap to corner but that's less of a problem now. Below is my code, and it plane doesn't work. There are no errors, it just won't pick up my clicking and dragging. I assume it has something to deal with the camera and ray casting because I know little about them. If you know a fix, please help, if you can explane how it all works too that would be nice.

 var moving = false;
 var posX : float;
 var posY : float;
 var ray : Ray;
 ray  = Camera.main.ScreenPointToRay (Input.mousePosition);
 function update () {
     
     if(moving == true){
         //The Object is 14 away from the camera,
         Debug.Log(ray.GetPoint(14));
         rigidbody.MovePosition(rigidbody.position + ray.GetPoint(14) * Time.deltaTime);
         //transform.position.x = ray.GetPoint(14).x;
           //transform.position.y = ray.GetPoint(14).y;
           //transform.position.z = 1.1;
     }
 }
 
 function OnMouseDown () {
    moving = true;
 }
 
 function OnMouseUp () {
       moving = false;
   //Snap to feature
   posX = transform.position.x;
   Mathf.Round(posX);
   if(posX > transform.position.x){
       posX--;
   }
    posY = transform.position.y;
   Mathf.Round(posY);
   if(posY > transform.position.y){
       posY--;
   }
   transform.position.x = posX;
   transform.position.y = posY;
 }
 
 function OnCollisionEnter () {
     rigidbody.isKinematic = true;
 }
 
 function OnCollisionExit () {
     rigidbody.isKinematic = false;
 }

Please note, I've tried using the DragObject from the wiki page, but first this is meant to be on an XY plane, and I'm not sure how to fix that properly.

Comment
Add comment · Show 9
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 whydoidoit · May 21, 2012 at 06:16 PM 0
Share

Have you attached a collider to the thing you want to click and drag?

avatar image easilyBaffled · May 21, 2012 at 08:11 PM 0
Share

Its a rigidbody if that's what you mean.

avatar image whydoidoit · May 21, 2012 at 08:15 PM 0
Share

No I mean does it have a box collider, a mesh collider etc?

avatar image easilyBaffled · May 21, 2012 at 08:19 PM 0
Share

Yes the its a standard box with the usual box collider

avatar image whydoidoit · May 21, 2012 at 08:22 PM 0
Share

Right I am just shooting at the low hanging fruit still, but your Update function has a lowercase "u" as well - not sure that will run.

Just to be clear - if you stick debug logging in each of those routines, what happens?

Show more comments

2 Replies

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

Answer by whydoidoit · May 21, 2012 at 08:43 PM

Ok to summarise the answer:

The Update function needed a capital U. The kinematic property needed to be set in the OnMouseXXXX functions and the rigidbody needed to be set to the ray.GetPoint(14) not moved by it.

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

Answer by easilyBaffled · May 21, 2012 at 08:52 PM

Here's the working code, for anyone that wants to use it. If you can make it run better or smarter, please feel free to add to it.

 var moving = false;
 var posX : float;
 var posY : float;
 var ray : Ray;
 
 function Update () {
     
     if(moving == true){
         ray  = Camera.main.ScreenPointToRay (Input.mousePosition);
         //The Object is 14 away from the camera,
         Debug.Log(ray.GetPoint(14));
         //rigidbody.MovePosition(rigidbody.position + ray.GetPoint(14) * Time.deltaTime);
         transform.position.x = ray.GetPoint(14).x;
           transform.position.y = ray.GetPoint(14).y;
           transform.position.z = 1.1;
     }
 }
 
 function OnMouseDown () {
    moving = true;
 }
 
 function OnMouseUp () {
       moving = false;
   //Snap to feature
   posX = transform.position.x;
   posX = Mathf.Round(posX);
   if(posX > transform.position.x){
       posX--;
   }
    posY = transform.position.y;
   posY = Mathf.Round(posY);
   if(posY > transform.position.y){
       posY--;
   }
   transform.position.x = posX + .5;
   transform.position.y = posY + .5;
 }
 
 function OnCollisionEnter () {
     rigidbody.isKinematic = true;
 }
 
 function OnCollisionExit () {
     rigidbody.isKinematic = false;
 }
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

Unity 5 - Hex Tile change color on mouseover 0 Answers

Camera.ScreenToWorldPoint with Perspective camera 1 Answer

Mouse plane does not detect height 1 Answer

Why is my raycast2D offset from the mouse position? 0 Answers

Why is my Gameovject not at 0 in the z acis? 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