• 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
Question by fernandovt · Oct 10, 2013 at 08:17 PM · rigidbodyraycastcolliderdrag

Increase Drag Distance??

Hello:), I have a working script to drag objects that have a rigidbody (i found it on internet), It works to open doors by dragging them, or any object in my game with rigidbody. The proble is that it only drag things that are too close, and i need it to drag things like drawers that are closer to the ground. The only way i can drag them is increasing the size of their colliders, but that's not the right way, I know there must be a way in the script to change raycast distance or somethig. Any ideas??:)

var spring = 50.0; var damper = 5.0; var drag = 10.0; var angularDrag = 5.0; var distance = 0.2; var pushForce = 0.2; var attachToCenterOfMass = false;

var highlightMaterial : Material; private var highlightObject : GameObject;

private var springJoint : SpringJoint;

function Update() { var mainCamera = FindCamera();

 highlightObject = null;
 if( springJoint != null && springJoint.connectedBody != null )
 {
     highlightObject = springJoint.connectedBody.gameObject;
 }
 else
 {
     // We need to actually hit an object
     var hitt : RaycastHit;
     if( Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hitt, 3.5 ) ) {
         if( hitt.rigidbody && !hitt.rigidbody.isKinematic ) {
             highlightObject = hitt.rigidbody.gameObject;
         }
     }
 }
 
     
 // Make sure the user pressed the mouse down
 if (!Input.GetMouseButtonDown (0))
     return;

     
 // We need to actually hit an object
 var hit : RaycastHit;
 if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, 3.5)) {
     return;
 }
 
 hit.transform.gameObject.SendMessage("OnBeingDragged", SendMessageOptions.DontRequireReceiver);
 
 // 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");
     body = go.AddComponent ("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;
 
 DragObject(hit.distance, hit.point, mainCamera.ScreenPointToRay(Input.mousePosition).direction);

}

function DragObject (distance : float, hitpoint : Vector3, dir : Vector3) { var startTime = Time.time; var mousePos = Input.mousePosition;

 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 (Mathf.Abs(mousePos.x - Input.mousePosition.x) <= 2 && Mathf.Abs(mousePos.y - Input.mousePosition.y) <= 2 && Time.time - startTime < .2 && springJoint.connectedBody)
 {
     dir.y = 0;
     dir.Normalize();
     springJoint.connectedBody.AddForceAtPosition(dir * pushForce, hitpoint, ForceMode.VelocityChange);
     ToggleLight( springJoint.connectedBody.gameObject );
 }    
 
 
 if (springJoint.connectedBody)
 {
     springJoint.connectedBody.drag = oldDrag;
     springJoint.connectedBody.angularDrag = oldAngularDrag;
     springJoint.connectedBody = null;
 }

}

static function ToggleLight( go : GameObject ) {
var theLight : Light = go.GetComponentInChildren(Light); if( !theLight ) return;

 theLight.enabled = !theLight.enabled;
 var illumOn = theLight.enabled;
 var renderers = go.GetComponentsInChildren(MeshRenderer);
 for( var r : MeshRenderer in renderers )
 {
     if( r.gameObject.layer == 1 )
     {
         r.material.shader = Shader.Find(illumOn ? "Self-Illumin/Diffuse" : "Diffuse");
     }
 }

}

function FindCamera () { if (camera) return camera; else return Camera.main; }

function OnPostRender() { if( highlightObject == null ) return;

 var go = highlightObject;
 highlightMaterial.SetPass( 0 );
 var meshes = go.GetComponentsInChildren(MeshFilter);
 for( var m : MeshFilter in meshes )
 {
     Graphics.DrawMeshNow( m.sharedMesh, m.transform.position, m.transform.rotation );
 }

}

Comment

People who like this

0 Show 0
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Raycast and Instantiate 1 Answer

How to prevent objects from interpenetrating eachother ? 1 Answer

Raycast doesn't detect the ground when the player is on a ledge. 1 Answer

How to Stop a Game Object from Passing through a Collider while it's being Dragged? 3 Answers

How to raycast a child collider of a parent rigidbody 2 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