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 /
This question was closed Apr 07, 2013 at 08:43 PM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Nonameghost · Apr 07, 2013 at 03:14 PM · camerainputmousedraggingrays

Simple Object Dragging

Hello all. I'm at a game jam right now, time's running low, and I'm stuck with this object dragging.

//////

 using UnityEngine;
 using System.Collections;
 
 public class Dragable : MonoBehaviour {
     
     bool dragging;
     RaycastHit hit;
 
     // Use this for initialization
     void Start () {
         dragging = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit, Mathf.Infinity)){
             Debug.Log ("Clicked Biyatch!");
             dragging = true;
         }
         if (dragging == true){
             Vector3 mpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             float tx = mpos.x;
             float ty = mpos.y;
             rigidbody.MovePosition(new Vector3(tx,ty,transform.position.z));
         }
     }
     
     void onMouseDown() {
         Debug.Log("Click");
         if (dragging == false){
             dragging = true;
         }
     }
         
     void onMouseUp(){
         if (dragging == true) {
             dragging = false;
         }
     }
 }

This registers a click, and it's supposed to update the object position to the mouse x/y. It's crude but I'm trying to do it quickly so I can move on.

What winds up happening is when I click, it sets the transform to the center of the camera. Can't find why it's doing that. Am I using anything incorrectly?

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 Nonameghost · Apr 07, 2013 at 12:31 PM 0
Share

Figured it out, line 21 needs to be "Vector3 mpos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, $$anonymous$$athf.Abs(transform.position.z - Camera.main.transform.position.z)));" The z position was way in the background, which messed up my mouse pos value.

0 Replies

  • Sort: 

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

Need help on the 3ds max style camera control 0 Answers

drag to move object in perspective camera 0 Answers

Mouse dragging detection 0 Answers

Strange Camera Behavior on iOS - Runs perfect in Unity 0 Answers

Move camera with mouse (2D) 1 Answer


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