Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by SpaceBatman24 · Mar 22, 2020 at 06:28 PM · raycastingmouseclickdestroy object

How to destroy specific gameobject with a mouse click once player is looking at them using a Ray

Hi,

So I have a scene set up where the end goal is to get within range of the villain, look at them, and click to destroy them with the mouse (not necessarily with the mouse cursor pointed on them, just with the player character looking at them with the raycast). I have been able to set it up to where the enemy is destroyed on click no matter where the player is, however I want this to only occur once the ray is looking at them. Could anyone help me figure out how to create this code? (I am also very new to coding so I am not familiar with all of the functions yet)

Hero code to cast the ray:

     Ray r = new Ray();
     r.origin = gameObject.transform.position;
     r.direction = gameObject.transform.forward;

     Debug.DrawLine(r.origin, r.origin + r.direction * 10.0f, Color.magenta);

     RaycastHit rhit;

     if (Physics.Raycast(r, out rhit, 10.0f))
     {
         if (rhit.collider.gameObject.tag == "Enemy")
         {
             gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
             
            /* if(Input.GetMouseButtonDown(0))
             {
                 Destroy(gameObject);
             }*/
         }
         else
         {
             gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
         }
     }

Villain code to be destroyed on click:

   void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Destroy(gameObject);
         }
     }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Mar 22, 2020 at 06:44 PM

Hello.

You are very close!

Lets see the RAYCAST manual :https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

IF you look all the page, will see there are different ways to "send" a ray. But If you think a little, will realize you always need the same, an origin for the ray, and a direction+distance or a destination. If not is impossible for Unity to do any ray..

In your code you are using this:

 if (Physics.Raycast(r, out rhit, 10.0f))

So, you are saying: if Generating the ray "r" with max distance of "10", if hits something, return me true and "rhit" will be the point where the ray hits.

BUUT What is ray "r" ? You diddnt defined it in your code! For Unity, ray "r" is nothing, so Unity is casting a null ray, so is casting nothing!!

You need to define the ray first! OR you can use the Raycast function directly with the origin and the direction, like this:

 if (Physics.Raycast(Vector3 origin, Vector3 direction, float maxDistance))

I recommend you to learn to use raycasts before continue. Do a simple object with a script only for test raycast things until you domine it.

Good luck!

Comment
Add comment · 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

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

126 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click Point From Raycasting Not Yielding Expected Coordinates 1 Answer

Use raycast to click on object 3 Answers

I am stuck and don't know what to do, After upgrading to 2019 mouse and raycast don't work well 1 Answer

question about instantiating 2 Answers

How to create gameobjects by clicking in the scene view in the editor? 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