- Home /
destroy and create objects
Hey, I have made a script attached to a box collider that is positioned in front of the player. With the script I have so far made it possible for the player to create objects with the mouse buttons.
but I have no idea how to make that the player can also destroy objects that are infront of him/her.
EDIT: I've updated the script (the last code section) with a solution that is (what I believe) to be the right direction. but something is missing, anyone of you who knows what?
here is the script:
using UnityEngine;
using System.Collections;
public class spawnscript : MonoBehaviour {
public GameObject cubeprefab;
public GameObject rectangleprefab;
public GameObject stairprefab;
public GameObject spawncube;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("mouse 0"))
{
if(idea.Instance.shadow1 == true)
{
//Vector3 position = new Vector3(camera.transform.position.x + 2, camera.transform.position.y);
Instantiate(cubeprefab, transform.position, transform.rotation);
//Physics.IgnoreCollision(cubeprefab.collider, spawncube.collider);
}
}
if(Input.GetKeyDown("mouse 1"))
{
if(idea.Instance.shadow2 == true)
{
Instantiate(rectangleprefab, transform.position, transform.rotation);
}
}
if(Input.GetKeyDown("mouse 2"))
{
if(idea.Instance.shadow3 == true)
{
Instantiate(stairprefab, transform.position, transform.rotation);
}
}
if(Input.GetMouseButtonUp(0))
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if(Physics.Raycast(transform.position,fwd,10))
{
Destroy(hit.transform.gameObject);
}
}
}
}
Answer by gfr · Oct 19, 2011 at 11:24 AM
If your objects already have colliders, you could use e.g. `OnMouseUpAsButton()` on them and destroy it from there. Alternatively you could raycast on mouseclick and if you hit a destructible object, remove it instead of creating a new one. E.g.:
if(Input.GetMouseButtonUp(0))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Destroy(hit.transform.gameObject);
} else {
// instantiate new one
}
// ...
To restrict what can be destroyed use layer masks for your raycasts or add additional conditions like checking for tags etc.
how would you code if you'll try with the raycast solution? cuase I want to be able to destroy anything with a rigid body
if you're using C#, you need to put the keyword 'out' before hit. So,
Physics.Raycast(transform.position, fwd, out hit, 10);
Your answer
Follow this Question
Related Questions
objects not reappearing 2 Answers
Should I destroy and create objects from prefabs to create a "copying" gameplay mechanic? 0 Answers
How to activate an object when another object deactivates? 1 Answer
Destroy All Objects In The Scene??? 1 Answer
How to iterate all prefabs in project and create them in scene 2 Answers