- Home /
I want to change the animation of an object when it hits any object in a layer
As I said in the title, I am trying to make so the object changes the animation when it hits an object in the ground layer, I have tried with void OnCollisionEnter but I didn`t get the syntax and don`t know how to apply in my situation maybe by referencing in Unity the layer but I don`t know how to do that, (I am new in coding so it can be just me being dumb) ,As I said in the title, I am trying to make so the object changes the animation when it hits something in the ground layer not only one object, but any object in this layer, I have tried with void OnCollisionEnter but I didn`t get the syntax and don`t know how to apply in my situation, (I am new in coding so it can be just me being dumb)
can you post the code ? it will help to solve the problem and also have u checked if the collider is trigger ?
Answer by Ta1kur1 · Dec 05, 2021 at 07:11 PM
I think that this would work but im not sure because i have not tested it
public class Example : MonoBehaviour
{
private Animator anim;
[SerializeField] LayerMask groundLayer;
void Start()
{
anim = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.gameObject.layer == groundLayer)
{
//play the animation
anim.SetBool("example", true);
}
}
}
Your answer
Follow this Question
Related Questions
How to get all layers included in a LayerMask? 4 Answers
Flare showing in cameras that should not show it 1 Answer
Enable disable layerMask 0 Answers
Know if layer is in use 1 Answer
How does Raycast layer mask work? 1 Answer