Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGreg Schwartz
17,521 PointsWhen 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);
}
}