Help me edit this C# script?

This is my script which allows the player to pick up rigid body objects with holding left click, then drop them when the button is released. (I didn’t write this myself.)

using UnityEngine;
using System.Collections;

public class CursorScript : MonoBehaviour {
   
    float drag = 1.0f;
    float angularDrag = 5.0f;
    bool attachToCenterOfMass = false;
   
    private FixedJoint fixedJoint;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void OnGui()
    {
        GUILayout.BeginVertical ();
        if (Input.GetKeyDown (KeyCode.Escape)) {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
    }

    void Update() {



       
        if (!Input.GetMouseButtonDown (0))
            return;
       
        var mainCamera = FindCamera();
       
       
        RaycastHit hit;
        if (!Physics.Raycast(mainCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)), out hit, 3))
            return;
       
       
        if (!hit.rigidbody || hit.rigidbody.isKinematic)
            return;
       
        if (!fixedJoint) {
           
            GameObject go = new GameObject("Rigidbody dragger");
            Rigidbody body = go.AddComponent<Rigidbody>() as Rigidbody;
            fixedJoint = go.AddComponent<FixedJoint>() as FixedJoint;
            body.isKinematic = true;
        }
       
        fixedJoint.transform.position = hit.point;
        if (attachToCenterOfMass) {
           
            Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
            anchor = fixedJoint.transform.InverseTransformPoint(anchor);
            fixedJoint.anchor = anchor;
        } else {
            fixedJoint.anchor = Vector3.zero;
        }
       
        fixedJoint.connectedBody = hit.rigidbody;
       
        StartCoroutine("DragObject", hit.distance);
    }
   
    IEnumerator DragObject (float distance) {
       
        float oldDrag = fixedJoint.connectedBody.drag;
        float oldAngularDrag = fixedJoint.connectedBody.angularDrag;
       
        fixedJoint.connectedBody.drag = drag;
        fixedJoint.connectedBody.angularDrag = angularDrag;
       
        Camera mainCamera = FindCamera();
       
        while (Input.GetMouseButton (0)) {
           
            var ray = mainCamera.ScreenPointToRay (new Vector2(Screen.width / 2, Screen.height / 2));
            fixedJoint.transform.position = ray.GetPoint(distance);
            yield return null;
        }
       
        if (fixedJoint.connectedBody) {
           
            fixedJoint.connectedBody.drag = oldDrag;
            fixedJoint.connectedBody.angularDrag = oldAngularDrag;
            fixedJoint.connectedBody = null;
        }
    }
   
    Camera FindCamera () {
        if (GetComponent<Camera>())
            return GetComponent<Camera>();
        else
            return Camera.main;
    }
}

It works fine, but I was wondering how I could edit this so that the object is thrown instead of dropped? I’m not very good with C# so I don’t know where to begin.

This should put you in the right direction, as you have got rigidbodies:
https://unity3d.com/learn/tutorials/modules/beginner/physics/addforce

Also, it is always good to read a not self-written code like a book in another language. Read through it and check everyword you do not understand. So you know, what is going on in the code and where you can change things to achieve what you need.

1 Like

Turns out, using addforce needs a reference to an object, so I could only attach this script to objects directly.

Problem with this is, the object only goes in one fixed direction, and I’m not sure how it could go in the direction of the camera.

I will give you somes hint so you can work it out yourself.

Look at this line:

rb.AddForce (transform.forward * 20);

That is using the current objects “forward” vector - the current direction it is pointing in. It is multiplying that by 20 to give it some more force. You need to change the transform.forward to use your cameras transform.forward.

Second hint,

Camera.main.transform.forward

This is your currently active (main) cameras forward direction.

Good luck.

1 Like

Thank you all! I got it to work lol
You have to add a public camera variable and reference the camera in order to get it to work
Oi, why can’t C# have global variables?

Even if it is from 2011, this should help you with global var options :slight_smile: