- Home /
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?
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!
@UseDreams how are you getting on with this? If this has solved your problem would you $$anonymous$$d accepting the answer, if you're still fighting with it then comment below!