Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

DragRigidbody Distance <= error

Discussion in 'Scripting' started by Luke3671, Mar 12, 2015.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    52
    Heya, Unity have this free script, But want i am having trouble with is, You can click on the block from any where around the map, I don't want this, I want it so "Player" as to find how to get it first then able to click and drag, I've messed around with the vaules in the code but no luck,

    The code i'm trying to add it:
    if(distance <= 30){
    //drag rigidbody
    }

    but having errors bad


    If you can help be great code below
    thank you.


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. namespace UnityStandardAssets.Utility
    6. {
    7.     public class DragRigidbody : MonoBehaviour
    8.     {
    9.         const float k_Spring = 50.0f;
    10.         const float k_Damper = 5.0f;
    11.         const float k_Drag = 10.0f;
    12.         const float k_AngularDrag = 5.0f;
    13.         const float k_Distance = 0.2f;
    14.         const bool k_AttachToCenterOfMass = false;
    15.  
    16.         private SpringJoint m_SpringJoint;
    17.  
    18.  
    19.         private void Update()
    20.         {
    21.             // Make sure the user pressed the mouse down
    22.             if (!Input.GetMouseButtonDown(0))
    23.             {
    24.                 return;
    25.             }
    26.  
    27.             var mainCamera = FindCamera();
    28.  
    29.             // We need to actually hit an object
    30.             RaycastHit hit = new RaycastHit();
    31.             if (
    32.                 !Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin,
    33.                                  mainCamera.ScreenPointToRay(Input.mousePosition).direction, out hit, 100,
    34.                                  Physics.DefaultRaycastLayers))
    35.             {
    36.                 return;
    37.             }
    38.             // We need to hit a rigidbody that is not kinematic
    39.             if (!hit.rigidbody || hit.rigidbody.isKinematic)
    40.             {
    41.                 return;
    42.             }
    43.  
    44.             if (!m_SpringJoint)
    45.             {
    46.                 var go = new GameObject("Rigidbody dragger");
    47.                 Rigidbody body = go.AddComponent<Rigidbody>();
    48.                 m_SpringJoint = go.AddComponent<SpringJoint>();
    49.                 body.isKinematic = true;
    50.             }
    51.  
    52.             m_SpringJoint.transform.position = hit.point;
    53.             m_SpringJoint.anchor = Vector3.zero;
    54.  
    55.             m_SpringJoint.spring = k_Spring;
    56.             m_SpringJoint.damper = k_Damper;
    57.             m_SpringJoint.maxDistance = k_Distance;
    58.             m_SpringJoint.connectedBody = hit.rigidbody;
    59.  
    60.             StartCoroutine("DragObject", hit.distance);
    61.         }
    62.  
    63.  
    64.         private IEnumerator DragObject(float distance)
    65.         {
    66.             var oldDrag = m_SpringJoint.connectedBody.drag;
    67.             var oldAngularDrag = m_SpringJoint.connectedBody.angularDrag;
    68.             m_SpringJoint.connectedBody.drag = k_Drag;
    69.             m_SpringJoint.connectedBody.angularDrag = k_AngularDrag;
    70.             var mainCamera = FindCamera();
    71.             while (Input.GetMouseButton(0))
    72.             {
    73.                 var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    74.                 m_SpringJoint.transform.position = ray.GetPoint(distance);
    75.                 yield return null;
    76.             }
    77.             if (m_SpringJoint.connectedBody)
    78.             {
    79.                 m_SpringJoint.connectedBody.drag = oldDrag;
    80.                 m_SpringJoint.connectedBody.angularDrag = oldAngularDrag;
    81.                 m_SpringJoint.connectedBody = null;
    82.             }
    83.         }
    84.  
    85.  
    86.         private Camera FindCamera()
    87.         {
    88.             if (GetComponent<Camera>())
    89.             {
    90.                 return GetComponent<Camera>();
    91.             }
    92.  
    93.             return Camera.main;
    94.         }
    95.     }
    96. }
    97.  
     
  2. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    52
    Anyone able to help?
     
  3. splitter20

    splitter20

    Joined:
    Apr 29, 2015
    Posts:
    10
    On line #33 change the 100 to your desired ray length.