• 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
Question by UseDreams · Mar 06, 2021 at 11:47 AM · buttonsounddestroy objectpress

how to make the character look at the object and when clicked on mouse0 the object is removed with sound?

how to make the character look at the object and when clicked on mouse0 the object is removed with sound?

Comment

People who like this

0 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 jackmw94 · Mar 06, 2021 at 01:59 PM 0
Share

Are you working in 2D or 3D?

avatar image UseDreams jackmw94 · Mar 06, 2021 at 02:29 PM 0
Share

i working 3d

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by jackmw94 · Mar 06, 2021 at 10:24 PM

The way you'd do this is with IK, or inverse kinematics. This does depend on IK being setup for your model, if you downloaded it and it's rigged then you're probably fine but if you've made it yourself then you'll have to set it up and that's a problem for another answerer :D

Assuming that's all fine then your animator will require one of its layers to have the IK pass option checked, as seen in this screenshot: https://docs.unity3d.com/uploads/Main/AnimatorControllerToolSettingsIKPass.png

Just like Start or Update, monobehaviours also have a function called OnAnimatorIK in which you can handle IK changes:

 private void OnAnimatorIK()
 {
     animator.SetLookAtWeight(1);
     animator.SetLookAtPosition(lookObj.position)
 }


If lookObj is the object you want to look at then this will cause your character too look at its position. The look at weight doesn't have to be 1, if you feel like this is too much of a glare and you want more of a glance then adjust this down as you see fit.


Clicking an object and removing it with sound is a separate matter. To do this use a raycast - where you specify a point and a direction then cast a line, or ray, out until you hit something. There's a slightly awkward step in converting your mouse position on screen to a ray that originates at your camera but this is the method you'd use to detect whether an object gets clicked:

 if (Input.GetMouseButtonDown(0))
 {
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      RaycastHit hit;
      
      if (Physics.Raycast(ray, out raycastHit, 1000))
      {
            if (raycastHit.transform == transform)
            {
                // handle sound!
                Destroy(gameObject);
            }
      }
 }


This will destroy an object when clicked - assuming it has some sort of collider on. When you click add component and type collider, you'll see the different types of colliders you can add. Mesh colliders look like the obvious solution but are expensive so should be used with caution, it might be better to use one of the shape colliders to approximate or even create a lower poly mesh to use as the mesh collider. Now I'm typing this it sounds a bit intricate but whatever, the point is you need a collider :)

I put handle sound in a comment because the way you would usually handle playing sounds is to have an audio source component then call Play() on it when you want it to play. However if you are destroying your gameobject then your audio source will be destroyed too! You have a couple options here: you can disable your gameobject immediately and set it to destroy after a delay to give your audio a chance to play. You can have a separate SFX manager on a different gameobject, you can tell this to play your audio so that your object can destroy itself in peace. Or AudioSource has a static PlayClipAtPoint method where you can pass in your audio clip and a position.

Let me know if you need clarification for any of this!

Comment

People who like this

0 Show 1 · 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 jackmw94 · Mar 09, 2021 at 09:39 AM 0
Share

@UseDreams how are you getting on with this? If this has solved your problem would you mind accepting the answer, if you're still fighting with it then comment below!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 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 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

how to make the character look at the object and when clicked on mouse0 the object is removed with sound?(3D) 1 Answer

how to make the character look at the object and when clicked on mouse0 the object is removed with sound?(3D) 1 Answer

How To play sound with a keypress 2 Answers

Play sound on button press? 1 Answer

Checking to see how many times a button was pressed in a certain amount of time? 2 Answers


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