Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by afmj · Jul 28, 2019 at 10:28 PM · movementmousedesktop

Move object away or closer while dragging with mouse

This is my first post and i m new to Unity so i ' m sorry if i do something wrong.

Im working on a script to move an object with the mouse. The requierments are to:

  1. Move the object with the mouse

  2. To rotate the object

  3. The object must interact with other object (collision)

  4. To move the object away or closer to the camera while dragging it with the mouse.

I'm following this script that i found: https://sharpcoderblog.com/blog/drag-rigidbody-with-mouse-cursor-unity-3d-tutorial

i have made some adjustments and added a way to rotate the mouse, this is what i have so far:

 public class MoveObject : MonoBehaviour
 {
     #region Properties
     public float forceAmount = 1000;
     private Rigidbody selectedRigidbody;
     private Camera targetCamera;
     private Vector3 originalScreenTargetPosition;
     private Vector3 originalRigidbodyPos;
     private float selectionDistance;
     //my try with key movement
     /*------------------------------*/
     private Vector3 verticalMovement;
     /*---------------------------------*/
     #endregion
 
     #region Events
     // Start is called before the first frame update
     void Start()
     {
         targetCamera = GetComponent<Camera>();
         verticalMovement = new Vector3();
     }
 
     void Update()
     {
         if (!targetCamera)
             return;
 
         if (Input.GetButtonDown("Fire 1"))
         {
             //Revisa si al presionar se tiene un Rigidbody
             selectedRigidbody = GetRigidbodyFromMouseClick();
         }
         if (Input.GetButtonUp("Fire 1") && selectedRigidbody)
         {
             //Libera al objeto que se esta moviendo
             verticalMovement = new Vector3();
             selectedRigidbody.velocity = new Vector3();
             selectedRigidbody = null;
         }
         if (Input.GetButtonUp("Rotation") && selectedRigidbody)
         {
             //Rotacion del Objeto en el eje y
             var yRotation = 0.0f;
             if (Input.GetAxis("Rotation") > 0) yRotation = 90.0f;
             if (Input.GetAxis("Rotation") < 0) yRotation = -90.0f;
             selectedRigidbody.transform.Rotate(0, yRotation, 0);
         }
     }
 
     void FixedUpdate()
     {       
         if (selectedRigidbody)
         {
             /*-------------------------------------*/
             if (Input.GetButton("Vertical"))
             {
                 //movimiento vertical de objeto
                 if (Input.GetAxis("Vertical") > 0)
                 {
                     verticalMovement = targetCamera.transform.forward;
                 }
                 else
                 {
                     verticalMovement = -targetCamera.transform.forward;
                 }
             }
             /*--------------------------------------------------*/  
             Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
             Debug.Log(mousePositionOffset);
             selectedRigidbody.velocity = (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position + verticalMovement)  * forceAmount * Time.deltaTime;
 
             //Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
             //selectedRigidbody.velocity = (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position) * forceAmount * Time.deltaTime;
 
         }
     }
     #endregion
 
     #region Methods
     Rigidbody GetRigidbodyFromMouseClick()
     {
         RaycastHit hitInfo = new RaycastHit();
         Ray ray = targetCamera.ScreenPointToRay(Input.mousePosition);
         bool hit = Physics.Raycast(ray, out hitInfo);
         if (hit)
         {
             if (hitInfo.collider.gameObject.GetComponent<Rigidbody>())
             {
                 selectionDistance = Vector3.Distance(ray.origin, hitInfo.point);
                 originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));
                 originalRigidbodyPos = hitInfo.collider.transform.position;
                 return hitInfo.collider.gameObject.GetComponent<Rigidbody>();
             }
         }
         return null;
     }
     #endregion
 }


i added the private Vector3 verticalMovement; and the targetCamera.transform.forward to try to move the object with the w (foward) and s (back) key. The object does move but i think the mouse offset does not update the distance position so the movement does not work that well.

Basic information about the "project"

Imagine a room where you are standing in the middle of it and you can only rotate the camera (with the mouse), you can move objects (there are multiple) around to build something like a living room. alt text

basically i was wondering what i could do to make it work? or if the way i ' m handling this is incorrect and there's a much better way of doing this?

sin-titulo.png (86.4 kB)
sin-titulo2.png (84.6 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to make a Smooth grid based movement with mouse/touch drag 0 Answers

Input.GetAxis going crazy when I click outside the game window 0 Answers

How can I use both mouse and keyboard to do the same thing? [SOLVED] 1 Answer

Using MoveTowards to move GameObject from starting point to mouse cursor 0 Answers

How to unable player to move on GUI buttons 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