[英]Playing an animation on a trigger collider
So I have looked this up, yet nothing worked for me. 因此,我对此进行了查找,但对我没有任何帮助。 I have an animation, called 'gemAnimation1', and I have a box collider set to trigger. 我有一个名为“ gemAnimation1”的动画,并且有一个设置要触发的盒对撞机。 When it collides with the player, I want it to switch animations. 当它与播放器碰撞时,我希望它切换动画。 This is the code I pieced together, but it doesn't work. 这是我拼凑的代码,但是没有用。 The trigger for the animation 'hasTouched'. 动画“ hasTouched”的触发器。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gemCollider : MonoBehaviour {
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update ()
{
}
void OnTriggerEnter (Collider other)
{
animator.SetTrigger("hasTouched");
}
}
EDIT 编辑
This is a screenshot of the gameobject the script is attached to. 这是脚本附加到的游戏对象的屏幕截图。
The OnTriggerEnter
function is for 3D GameObjects. OnTriggerEnter
函数适用于3D OnTriggerEnter
。
You are using SpriteRenderer
and BoxCollider2D
which are both for 2D GameObjects. 您正在使用都用于2D SpriteRenderer
和BoxCollider2D
。 The OnTriggerEnter2D
function with Collider2D
as argument must be used. 必须使用以Collider2D
作为参数的OnTriggerEnter2D
函数。
void OnTriggerEnter2D(Collider2D collision)
{
animator.SetTrigger("hasTouched");
}
Note: 注意:
You must also attach Rigidbody2D
to one of the 2D Objects in order for the OnTriggerEnter2D
function to be called. 您还必须将Rigidbody2D
附加到2D对象之一,以便调用OnTriggerEnter2D
函数。 I don't see Rigidbody2D
in your screenshot. 在您的屏幕截图中没有看到Rigidbody2D
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.