Wednesday, April 29, 2015

unity - 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();
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().ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
pickupable p = hit.collider.GetComponent();
if(p!=null)
{
carrying = true;
carriedObject = p.gameObject;
}

}
}
}

void carry(GameObject o)
{
o.GetComponent().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();
// 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().isKinematic = false;
gameObject.GetComponent().AddForce(carriedObject.transform.forward * 100);
// carriedObject.GetComponent().AddForce(0,0,1f);
//carriedObject.gameObject.GetComponent().isKinematic = false;
carriedObject = null;

}
}

Thanks in advance!




Answer



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();

}

// 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();
if (p != null)
{
carrying = true;
carriedObject = p.gameObject;
}
}
}

}

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

void ThrowBall()
{

carriedObject.GetComponent().isKinematic = false;
carriedObject.GetComponent().AddForce(0f, 0f, 100f);
}
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...