• 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 convictcartel · Feb 24, 2014 at 05:11 AM · rigidbodydistancedrag

Distance from rigid body and drag

I have the drag rigid body script, all I want to do is make it so you can only do this if you are close to the rigidbody and if you are your crosshair turns to a 2d image.. Help please and thank you! Heres my JS

 var normalCollisionCount = 1;
 var spring = 50.0;
 var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var throwForce = 500;
 var throwRange = 1000;
 var attachToCenterOfMass = false;
 var other : Transform;
 var crosshairFirstModeHorizontal : Texture2D;
     
 private var springJoint : SpringJoint;
  
 function Update ()
 {
     // Make sure the user pressed the mouse down
 //var dist = Vector3.Distance(!hit.rigidbody.position, transform.position);
 //if (dist < 1 ) {
     if (!Input.GetMouseButtonDown (0))
         return;
  
     var mainCamera = FindCamera();
  
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 100))
         return;
     // We need to hit a rigidbody that is not kinematic
     if (!hit.rigidbody || hit.rigidbody.isKinematic)
         return;
         
     if (!springJoint)
     {
         var go = new GameObject("Rigidbody dragger");
         var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
         springJoint = go.AddComponent ("SpringJoint");
         body.isKinematic = true;
     }
  
     springJoint.transform.position = hit.point;
     if (attachToCenterOfMass)
     {
         var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
         anchor = springJoint.transform.InverseTransformPoint(anchor);
         springJoint.anchor = anchor;
     }
     else
     {
         springJoint.anchor = Vector3.zero;
     }
  
     springJoint.spring = spring;
     springJoint.damper = damper;
     springJoint.maxDistance = distance;
     springJoint.connectedBody = hit.rigidbody;
  
     StartCoroutine ("DragObject", hit.distance);
 //}
 }
  
 function DragObject (distance : float)
 {
 
     var oldDrag = springJoint.connectedBody.drag;
     var oldAngularDrag = springJoint.connectedBody.angularDrag;
     springJoint.connectedBody.drag = drag;
     springJoint.connectedBody.angularDrag = angularDrag;
     var mainCamera = FindCamera();
     while (Input.GetMouseButton (0))
     {
         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
  
         if (Input.GetMouseButton (1)){
             springJoint.connectedBody.AddExplosionForce(throwForce,mainCamera.transform.position,throwRange);
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
     }
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
     
 }
  
 function FindCamera ()
 {
     if (camera)
         return camera;
     else
         return Camera.main;
 }
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 robertbu · Feb 24, 2014 at 05:21 AM 0
Share

This the the third question dealing with a modification to the DragRigidbody.js script in the last three days. Some unknows in your question.

First, what do you want to measure from and to for your distance. Camera position? Camera.near clip plane? Hit position? Pivot position of the object?

And I can only guess at what this means: "crosshair turns to a 2d image." Do you have code somewhere that is displaying and a crosshair? And you want that to change when? If the user is able to pick something up or if the user is dragging something?

Assuming you want the distance from the camera to the hit.point to be the measure, you can insert the following at line 32:

 if (Vector3.Distance(hit.point, Camera.main.transform.position) < someValue)
   return;   

You will only be able to drag something if the distance between the camera and the point hit is less than 'someValue', where 'someValue' is either a constant you insert or a variable you define and initialize elsewhere.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Check if object is moving towards a point 2 Answers

Help with DragRigidbody.js ? 1 Answer

Horizontal Drag, no Vertical Drag 1 Answer

Rotate object with rigidbody and high angular drag around particular local axis 0 Answers

How to build a XZ only rigidbody drag function in local space? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges