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 /
This question was closed Feb 28, 2014 at 11:49 PM by duclaw for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by duclaw · Feb 28, 2014 at 08:21 PM · rigidbodyraycastragdolldragrigidbody

modifying dragrigidbody script

Hello, i have been trying to make the drag rigidbody script work without a camera this meant i have to change some lines of code but i have gotten stuck at a particular point, i want to know how to achieve the same effect by using a different method.

i am trying to drag the rigid body(ragdoll) with an object(dragger) and i am stuck at this point in the code

 var dir  = Vector3.zero;// direction of the object to be dragged
 var target = Vector3.zero; // position of the object
 var myTransform: Transform;
 
 function Awake()
 {
       myTransform = transform; // for better performance
 }
 function Update ()
 {
     var dir = target - myTransform.position;
     Debug.DrawRay(myTransform.position, dir, Color.green);
     // Make sure the user pressed the mouse down
     if (!Input.GetMouseButtonDown (0))
         return;
     // check if the ragdoll can be carried    
     if(target == null)
     return;
     
     
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(myTransform.position,dir,  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;
     while (Input.GetMouseButton (0))
     {
 // the current code i am using
         var ray = new Ray (myTransform.positon, myTransform.forward);
 //var ray = main
         //var ray: Ray;
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
     }
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
 }

it gives me null exception reference anytime i run it NullReferenceException: Object reference not set to an instance of an object dragPrey+$DragObject$38+$.MoveNext () (at Assets/myScripts/dragPrey.js:83) UnityEngine.MonoBehaviour:StartCoroutine(String, Object) dragPrey:Update() (at Assets/myScripts/dragPrey.js:72)

please i would like to know what is wrong with code above thanks.

Comment
Add comment · Show 5
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 duclaw · Feb 28, 2014 at 10:51 PM 0
Share

i have edited it

avatar image robertbu · Feb 28, 2014 at 10:54 PM 0
Share

Thanks. If no one else gives you an answer, I'll look over it when I have a bit more time. I've helped several people make changes to this script in the last couple of weeks.

avatar image duclaw · Feb 28, 2014 at 11:38 PM 0
Share

thanks a lot

avatar image duclaw · Feb 28, 2014 at 11:49 PM 0
Share

thanks for your readiness to help me robertbu, i eventually found a way but we are not allowed to post answers to our questions so i am closing this, thanks a lot

avatar image robertbu · Feb 28, 2014 at 11:56 PM 0
Share

Occasionally there are browser issues, but generally anyone can post an answer to their own question. I'm glad you got it fixed.

0 Replies

  • Sort: 

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

2D Rigidbody Drag with Mouse 0 Answers

Realistic Bow and Arrow Physics? 2 Answers

change to raycast shooting instead of rigidbody shooting 1 Answer

Why is CapsuleCast/Raycast performance effected by rigidbody? 1 Answer

Raycast On Rigidbody 1 Answer


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