Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Claymore · Oct 17, 2012 at 12:01 PM · raycastcolliderhit

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Oct 17, 2012 at 03:21 PM 0
Share

+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.. )

avatar image Claymore · Oct 17, 2012 at 04:41 PM 0
Share

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. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Claymore · Oct 17, 2012 at 01:16 PM 0
Share

Can you write this in javascript please,and make it only destroys player (with tag) :) Thank you anyway for attention.

avatar image Claymore · Oct 17, 2012 at 01:57 PM 0
Share

Anyway script is not working.

avatar image Seth-Bergman · Oct 17, 2012 at 03:55 PM 0
Share

looks ok, but I think you would need to say:

  Destroy(hit.collider.gameObject); //gameObject is lowercase 
avatar image Claymore · Oct 17, 2012 at 04:28 PM 0
Share

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 :)

avatar image Seth-Bergman · Oct 17, 2012 at 04:43 PM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges