# Throwing a grabbed object

I'm trying to throw the carried object in the direction of the first person controller, but unable to make it. I could make the carried object drop, but the same time i need to instantiate velocity to the carried object, so that i could forward. I'm building up a bowling game and trying to make the ball behave like the same when the player throws the ball. It should hit the alley and move forward to hit the pins. Below is what so far I've tried.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pickupobject : MonoBehaviour {
GameObject mainCamera;
//public GameObject empty;
bool carrying;
public GameObject carriedObject;
// Camera cam;
public float distances;
public float smooth;
float speed = 1000f;

// Use this for initialization
void Start()
{
//cam = GameObject.Find("MainCamera").GetComponent<Camera>();
mainCamera = GameObject.FindWithTag("MainCamera");
}

// Update is called once per frame
void Update () {
if (carrying)
{
carry(carriedObject);
CheckDrop();
}
else
{
pickup();
}

}

private void pickup()
{
if(Input.GetKeyDown(KeyCode.E))
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
pickupable p = hit.collider.GetComponent<pickupable>();
if(p!=null)
{
carrying = true;
carriedObject = p.gameObject;
}
}
}
}

void carry(GameObject o)
{
o.GetComponent<Rigidbody>().isKinematic = true;
o.transform.position =  mainCamera.transform.position + mainCamera.transform.forward * distances;

}

//void ThrowBall()
//{
//    GameObject go = (GameObject)Instantiate(carriedObject,transform.forward, Quaternion.identity);
//    pickupable to = go.GetComponent<pickupable>();
//    to.Throw(carriedObject.transform.forward * speed);
//}

void CheckDrop()
{
if(Input.GetKeyDown(KeyCode.U))
{
Drop();
}
}

void Drop()
{
carrying = false;
// carriedObject = Instantiate(carriedObject, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
carriedObject.GetComponent<Rigidbody>().isKinematic = false;
//carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
carriedObject = null;

}
}


• I assume ThrowBall is the function responsible for throwing the carried object. First of all, you shouldn't be instantiating a new object in this function, instead, use the carriedObject_, second, for throwing an Object apply Force on it (Assuming it has Rigidbody component attached)) – Aditya Yadav Apr 25 '17 at 9:15
• I assume, you mean transform.position for second? and carriedObject.GetComponent<RigidBody>().AddForce(0,0,1f) for the Object? – Salar Muhammad Apr 25 '17 at 9:18
• I don't what you mean by your first question, but yeah, carriedObject.GetComponent<RigidBody>().AddForce("Direction Vector here") is what you want to use. – Aditya Yadav Apr 25 '17 at 9:33
• I mean to say, do i need to add this component as well, i.e transform.position? – Salar Muhammad Apr 25 '17 at 9:42
• No, you don't need that. – Aditya Yadav Apr 25 '17 at 9:53

Continuing my comment: you need some changes in your code to check when to throw the object.

I've updated the code to account for those changes, it should work as expected now.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pickupable : MonoBehaviour
{
//GameObject mainCamera;
bool carrying;
GameObject carriedObject;
Camera cam;
public float distances;
public float smooth;

// Use this for initialization
void Start()
{
cam = GameObject.Find("MainCamera").GetComponent<Camera>();
}

// Update is called once per frame
void Update()
{
//Check for user input ('T' key here) and make sure the object is being carried
if(Input.GetKeyDown(KeyCode.T) && carrying)
{
carrying = !carrying;
ThrowBall();
}
if (carrying)
{
carry(carriedObject);
}
else
{
pickup();
}

}

private void pickup()
{
if (Input.GetKeyDown(KeyCode.E))
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = cam.ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Pickupable p = hit.collider.GetComponent<Pickupable>();
if (p != null)
{
carrying = true;
carriedObject = p.gameObject;
}
}
}
}

void carry(GameObject o)
{
o.GetComponent<Rigidbody>().isKinematic = true;
o.transform.position = cam.transform.position + new Vector3(0f,0f, 10f);
}

void ThrowBall()
{
carriedObject.GetComponent<Rigidbody>().isKinematic = false;