Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_su5c8qsiI5iPAQ · Dec 14, 2021 at 07:22 AM · rigidbody2ddrag-and-drophowspringjointthrow

How to make drag and throw (2D)

Hi, I need to do drag and throw and I know there are lots of identical questions and generally the answer is SpringJoint or its types.


I tried joints but Im not satisfied with them, they stretch too much and/or circle around the mouse. I haven't found any way to fix this anywhere on internet.


So I have 2 questions:


How do Imake trully rigid spring joint?

And if I can't, what should I use for drag and throw?

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
0

Answer by metalted · Dec 14, 2021 at 01:20 PM

I never really tried making a drag and throw kind of mechanic, so I thought it would be a nice challenge. I didn't use any joints, just the rigidbody and the mouse input. It's a bit of a 10-min code jam, so embrace the jank I guess. What I did was use a raycast to detect the object and a mouse button to grab. While holding the object, store the previous' frame position and the current frames position. When releasing the mouse button use the two positions to calculate the drag direction and the distance (distance per frame = speed) and add some force to the rigidbody. Things I noticed already is that you might want to clamp the distance value so it doesn't fly off. Not saying this is the best or only solution, but it is a solution. Hope it helps you out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MouseDragThrow : MonoBehaviour
 {
     public GameObject currentlyDraggedObject = null;
     private float savedZPosition = 0;
     public Camera cam;
 
     private Vector3 previousDragPosition;
     private Vector3 currentDragPosition;
 
     public void Update()
     {
         RaycastHit hit;
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         Vector2 mousePosition = Input.mousePosition;
 
         if(Input.GetMouseButtonDown(0))
         {
             if(Physics.Raycast(ray, out hit))
             {
                 if(hit.collider.gameObject.tag == "Draggable")
                 {
                     currentlyDraggedObject = hit.collider.gameObject;
                     savedZPosition = Mathf.Abs(cam.transform.position.z - currentlyDraggedObject.transform.position.z);
                     currentlyDraggedObject.GetComponent<Rigidbody>().isKinematic = true;
                     currentDragPosition = currentlyDraggedObject.transform.position;
                 }
             }
         }         
 
         if(currentlyDraggedObject != null)
         { 
             currentlyDraggedObject.transform.position = cam.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, savedZPosition));
             previousDragPosition = currentDragPosition;
             currentDragPosition = currentlyDraggedObject.transform.position;
 
             if(Input.GetMouseButtonUp(0))
             {
                 Vector3 direction = currentDragPosition - previousDragPosition;
                 float distance = Vector3.Distance(previousDragPosition, currentDragPosition);
                 currentlyDraggedObject.GetComponent<Rigidbody>().isKinematic = false;
                 currentlyDraggedObject.GetComponent<Rigidbody>().AddForce(direction * distance * 10f, ForceMode.Impulse);
                 currentlyDraggedObject = null;
             }  
         }
     }  
 }
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

136 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

Related Questions

How can I change SpringJoint2D's anchor points in script? 1 Answer

Physics for multiple connections - advice needed 0 Answers

Connecting SpringJoint2D to Rotating Rigidbody2D With Correct Anchor 0 Answers

Bounce back after spinning an object once it hits the angle limit. 1 Answer

Drag rigid body based on touch in 2D 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