- Home /
Raycast Destroy(hit.collider.gameObject); (Still need help)
Hi,i want my player destroys on raycast,my first time working with raycast,so i just need script,i want my player destroys when he touches raycast,like:
if (hit.collider.gameObject.tag == "Player")
Destroy(hit.collider.gameObject);
Can someone please help me,been trying everything...Please someone help me...Ill be very thankful.
+1 for properly formatting your code!
ok, first off be patient, you will get help... I suspect a raycast is not the easiest way to accomplish what you are trying to do... What exactly are you trying to do? Where is the raycast co$$anonymous$$g from? can you provide the relevant code which contains the raycast?
(providing the relevant code that you have been trying would really help.. I've got no problem working back and forth with someone to help them learn if I can see they are actually trying.. Others here feel similar.. This question is a step in the right direction from your previous questions.. But this should be longer and more descriptive, so be SPECIFIC! what type of project are you working on, what have you got so far, what exactly is the part that isn't working.. If there are things you have been trying show examples.. )
Thanks for your good atention :) Anyway i just need to make player destroy when he touches raycast,i dont understand raycast,its my first time...Anyway im good at scripting,but in this opinion raycast is problem :) I just need help and i have to be patient. :)
Answer by KV-2 · Oct 17, 2012 at 01:14 PM
using UnityEngine;
using System.Collections;
public class testDestroy : MonoBehaviour {
private float distance = 100.0f;
void Update(){
Ray ray = new Ray(transform.position,transform.forward);
RaycastHit hit;
if (Physics.Raycast (ray,out hit,distance))
{
Destroy(hit.collider.GameObject);
}
}
}
Can you write this in javascript please,and make it only destroys player (with tag) :) Thank you anyway for attention.
looks ok, but I think you would need to say:
Destroy(hit.collider.gameObject); //gameObject is lowercase
Can you write a script for me please (I do not want to be shrill,but only if you want) Like example for me,cause raycast is not my side :)
this answer IS an example.. like I said, you would only need to lowercase the "GameObject" to fix it.. here it is in javascript as you asked:
private var distance : float = 100.0;
function Update(){
var ray : Ray = new Ray(transform.position,transform.forward);
var hit : RaycastHit;
if (Physics.Raycast (ray,out hit,distance))
{
Destroy(hit.collider.gameObject);
}
}
this ray would only destroy objects which have a collider, of course..
if you are hoping to build a game without actually learning how to use the engine, you are only hurting yourself.. You should be looking through tutorials, not asking for scripts
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer
Physics.Raycast not checking layermask properly? 1 Answer
Raycast hit working in unity editor but not in deployed webplayer build? 0 Answers
RaycastAll related question 1 Answer
Raycast ignoring tags problem 1 Answer