Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development VR in Unity Physics Scripting Powering Up Throwing Force

Greg Schwartz
Greg Schwartz
17,382 Points

When I click on Hands game object in the hierarchy in Unity, I don't see the option to assign the ThrowingObject script.

using UnityEngine; using System.Collections;

public class ThrowObject : MonoBehaviour {

[SerializeField]
private GameObject throwingObject;

private Rigidbody objectRigidBody;

private Vector3 startingPosition;
private Quaternion startingRotation;

private float objectForce = 0f;
private float maximumForce = 600f;
private float delay = 1f;
private float timer;


// Use this for initialization
void Start () {

    objectRigidBody = throwingObject.GetComponent<Rigidbody> ();
    startingPosition = throwingObject.transform.localPosition;
    startingRotation = throwingObject.transform.localRotation;
    objectRigidBody.isKinematic = true;
}

// Update is called once per frame
void Update () {


    //If the player has started to click the mouse button...
    if (Input.GetMouseButtonDown(0)){

        BeginThrow ();
    }

    // If the player is continuing to hold down the mouse button..
    if (Input.GetMouseButton(0)){

        PowerUpThrow ();

    }


    // If the player releases the mouse button...
    if (Input.GetMouseButtonUp(0)){

        ReleaseObject ();

    }


}



private void BeginThrow () {

    // Reset Physics
    objectRigidBody.isKinematic = true;
    objectRigidBody.velocity = Vector3.zero;
    objectRigidBody.angularVelocity = Vector3.zero;

}


private void ReleaseObject() {

    //launch the object.
    object.RigidBody.isKinematic = false;
    throwingObject.transform.parent = null;
    objectRigidBody.AddRelativeForce (throwingObject.transform.forward * objectForce);

    // Reset the force and timer.
    objectForce = 0f;
    timer = 0f;
}


private void PowerUpThrow (){

    // Increment timer oncer per frame
    timer += Time.deltaTime;
    if (timer > delay)
        timer = delay;

    // Lerp the object force the longer the player holds down the button.
    float perc = timer / delay;
    objectForce = Mathf.Lerp (0f, maximumForce, perc);

}

}