- Home /
Question by
Jdawg20072 · Jan 18, 2020 at 04:17 AM ·
3dfpsball
i'm new to unity. I want to make a script that creates and throws a ball. You can't throw the ball again until you pick it up. My throwing part works but the pick up action doesn't
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpObject : MonoBehaviour
{
GameObject prefab;
public GameObject prefab2;
public bool ballThrown = false;
void Start()
{
prefab = Resources.Load("projectile") as GameObject;
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
if (ballThrown == false)
{
GameObject projectile = Instantiate(prefab) as GameObject;
projectile.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody rb = projectile.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * 40;
ballThrown = true;
}
if(ballThrown == true)
{
}
}
}
void OnCollisionStay(Collision node)
{
if (node.gameObject.tag == "ball")
{
ballThrown = false;
Destroy(node.gameObject);
}
}
}
Comment
Answer by logicandchaos · Jan 18, 2020 at 11:59 AM
Rather than use a prefab and instantiate you should just make a public GameObject variable and assign it your ball in the inspector, then instead of destroying it you pick it back up.