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 StevenNieh · Feb 03, 2012 at 03:47 PM · physicsrigidbodyforcedrag

Add force based on drag speed

Hey everyone, I'm wondering how you can add force to a kinematic object based on the drag speed. Say the user is dragging a cube, and it's going to hit another cube. The faster of his dragging speed, the more force will push to another cube.

Thanks!

Steven

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Stardog · Aug 16, 2012 at 03:07 AM

It's not ideal, but this works. It will let you throw the object left/right/up/down in relation to the camera. Made for an FPS style view.

 Camera theCam = GameObject.FindGameObjectWithTag("MainCamera").camera;
 
 rigidbody.AddForce(theCam.transform.right * Input.GetAxis("Mouse X") * 10f, ForceMode.Impulse);
 rigidbody.AddForce(theCam.transform.up * Input.GetAxis("Mouse Y") * 10f, ForceMode.Impulse);

Add inside an OnMouseUp function after dragging, or similar.

rigidbody.isKinematic should ideally be false when dragging, then make it true before this code.

Here's a rough ready to use script:

 using UnityEngine;
 using System.Collections;
 
 public class DragDrop : MonoBehaviour {
     
     private Camera theCam;
     private bool dragging;
     
     void Start ()
     {
         theCam = GameObject.FindGameObjectWithTag("MainCamera").camera;
     }
     
     void LateUpdate ()
     {
         // This draws a line (ray) from the camera at a distance of 3
         // If it hits something, it moves the object to that point, if not, it moves it to the end of the ray (3 units out)
         if (dragging)
         {
             Ray ray = theCam.ScreenPointToRay(Input.mousePosition);
 
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 3f))
             {
                 transform.position = hit.point;
             }
             else
             {
                 transform.position = ray.GetPoint(3f);
             }
         }
     }
     
     void OnMouseDown()
     {
         dragging = true;
         rigidbody.isKinematic = true;
         collider.enabled = false;
     }
     
     // Adds the force when you let go based on the Mouse X/Y values.
     void OnMouseUp()
     {
         dragging = false;
         rigidbody.isKinematic = false;
         collider.enabled = true;
         
         rigidbody.AddForce(theCam.transform.right * Input.GetAxis("Mouse X") * 10f, ForceMode.Impulse);
         rigidbody.AddForce(theCam.transform.up * Input.GetAxis("Mouse Y") * 10f, ForceMode.Impulse);
     }
     
 }
 

To make it work without disabling the collider, you can use a LayerMask on the Raycast, or put the object onto the IgnoreRaycast layer.

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
0

Answer by Berenger · Feb 03, 2012 at 04:54 PM

You don't need to add the force yourself, the physic engine can take care of it. But of it to work, the cubes you're not dragging mustn't be kinematic, so I suggest you do the switch on mouse down. Also, as a mouse move can be pretty fast, use continuous dynamic collision detection on the dragged cube, and continuous on the others, so it won't just pass through.

If you really want to stick with all-kinematics, you can simulate a force by moving the object by a fading vector ( pos += forward * 5, *4, *3, *2.5 ... ), the vector being, on collision, pos - collider.pos. I doubt that it will be realistic though ...

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 StevenNieh · Feb 04, 2012 at 03:19 PM 0
Share

I did, but it doesn't work, please see the post below. Thanks!

avatar image
0

Answer by StevenNieh · Feb 04, 2012 at 06:09 PM

Hey Berenger,

Thanks for your reply.

I tried the code below:

 if (theTouch.phase == TouchPhase.Ended || theTouch.phase == TouchPhase.Canceled) {
   rigidbody.isKinematic = true;
   return;
 } else {
   rigidbody.isKinematic = false;
   var p:Vector3 = Camera.main.ScreenToWorldPoint(
    new Vector3 (theTouch.position.x, theTouch.position.y, 300));
   transform.position = p;
 }


It looks like the dragging cube can pass through other objects. I'm using continuous dynamic collision detection.

Thanks!

Steven

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
0

Answer by OleP · Feb 07, 2012 at 02:47 PM

Did you find an answer yet? I'm looking for kinda the same solution. I got a sphere in 2D(x,y), and I'm controling it by touch. When I stop my drag on the sphere, I want it to continue in the angle and speed it had, at the point of release. Furthermore it should maintain a realistic speed/velocity, and eventually stop.

So if you found a solution, please share it :)

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 StevenNieh · Feb 07, 2012 at 03:18 PM 0
Share

No i haven't found it yet.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Touch controlled ice puck 0 Answers

How to negate this force? 2 Answers

Direction of rotation and ApplyForce 1 Answer

Shooting a cannonball. 6 Answers

How exactly does Rigidbody Drag works? 3 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