- Home /
I need help making a trigger make an a game object so that it can be manipulated
Hello all, I am very new to scripting and am working on a project where a third person character is able to run around and pick up objects in the world and toss them. I had a previous script working that detected objects to be picked up via raycasting, but had a really hard time getting it to feel right since you can free look and orbit with the camera. Originally, the raycast came from an object in front of the character.
I'm experimenting with a new method for picking up objects where there is a large trigger space in front of the main character so that the player can clearly see what's in the pickup zone. I can't figure out how to identify the gameobject that is in the trigger zone so that it can take on the pickupable class and become carried. I'd like for the player to only be able to hold one object at a time. The "pickupable" class is a component that is on certain types of objects. You can also see in my code where I'm setting it up as a tag I can apply to prefabs, in case that will work better.
I've never posted before and have been looking at stuff trying to fix this all day. Hopefully you folks can help me. I'm attaching my pickup code. Thanks in advance. enter code here
using UnityEngine; using System.Collections;
public class betterPickUpObject : MonoBehaviour {
GameObject grabpoint;
GameObject holdpoint;
bool carrying;
bool held;
bool goodforpickup;
GameObject carriedObject;
public float distance;
public float smooth;
public float speed;
private float throwdirection;
public float pullspeed;
public GameObject MainCamera;
private Vector3 SpawnPosition;
private Vector3 dir;
// Use this for initialization
void Start() {
grabpoint = GameObject.FindWithTag("grabpoint");
holdpoint = GameObject.FindWithTag ("holdpoint");
MainCamera = GameObject.FindWithTag ("MainCamera");
}
// Update is called once per frame
void Update () {
if (carrying) {
carry (carriedObject);
}
else
{
pickup();
}
if (held) {
holding (carriedObject);
checkThrow ();
}
else
{
pickup();
}
}
void carry(GameObject o) {
dir = holdpoint.transform.position - carriedObject.transform.position;
dir = dir.normalized;
carriedObject.rigidbody.AddForce (dir * pullspeed);
o.rigidbody.isKinematic = false;
}
void OnTriggerEnter(Collider other){
carriedObject.transform.position = Vector3.Lerp (holdpoint.transform.position, holdpoint.transform.position, Time.deltaTime * smooth);
Debug.Log ("Object in Trigger");
{
if
(other.gameObject == carriedObject) {
held = true;
}
}
}
void holding (GameObject o){
carriedObject.transform.position = Vector3.Lerp (holdpoint.transform.position, holdpoint.transform.position, Time.deltaTime * smooth);
o.transform.Rotate (new Vector3 (15, 15, 0) * Time.deltaTime);
}
void OnTriggerEnter (Collision hit) {
if (hit.gameObject.tag == "Pickupable") {
Debug.Log ("Valid Object in Collider Box");
hit.gameObject =
goodforpickup = true;
}
}
void OnTriggerExit (Collision hit) {
if (hit.gameObject.tag == "Pickupable") {
Debug.Log ("Valid Object left Collider Box");
goodforpickup = false;
}
}
void pickup (){
if(Input.GetButtonDown("Fire1")) {
//int x = Screen.width / 2;
//int y = Screen.height /2;
if (goodforpickup = true){
//Ray ray = MainCamera.camera.ScreenPointToRay(new Vector3 (x,y));
//RaycastHit hit;
//if(Physics.Raycast(ray, out hit)){
Pickupable p = hit.GameObject.GetComponent<Pickupable>();
//Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
//Debug.DrawRay(transform.position, forward, Color.blue);
if(p != null){
carrying = true;
carriedObject = p.gameObject;
}
}
}
}
//void pickup(){
//if (Input.GetButtonDown("Fire1")) {
//int x = Screen.width / 2;
//int y = Screen.height /2;
//while (goodforpickup = true){
//Ray ray = MainCamera.camera.ScreenPointToRay(new Vector3 (x,y));
//RaycastHit hit;
//if(Physics.Raycast(ray, out hit)){
//Pickupable p = hit.collider.GetComponent<Pickupable>();
//Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
//Debug.DrawRay(transform.position, forward, Color.blue);
//if(p != null){
//carrying = true;
//carriedObject = p.gameObject;
void checkThrow (){
if (Input.GetButtonDown ("Fire1")) {
throwObject ();
}
}
void throwObject (){
carrying = false;
held = false;
carriedObject.gameObject.rigidbody.isKinematic = false;
//throwdirection = (MainCamera.transform.forward);
carriedObject.gameObject.rigidbody.AddTorque (Camera.main.transform.forward * speed);
carriedObject.gameObject.rigidbody.AddForce (Camera.main.transform.forward * speed);
//carriedObject.gameObject.rigidbody.AddForce
//(throwdirection * speed, 0, 0);
//carriedObject.gameObject.rigidbody.transform.TransformDirection (new Vector3 (speed, 0, 0));
carriedObject = null;
}
}