[英]Triggering unity animations based on GameObject proximity
I want to create a simple script that would trigger my animation when the player would step on a specific tile. 我想创建一个简单的脚本,当播放器踩到特定的图块时将触发我的动画。 However, it is not triggering! 但是,它不会触发!
To achieve this, I created an invisible (mesh renderer disabled) GameObject. 为此,我创建了一个不可见的(禁用网格渲染器)GameObject。 The animator, attatched to the group I'm animating defaults to an idle state. 隶属于我正在设置动画的组的动画师默认为空闲状态。
To said group, I apply this script: 对于上述小组,我应用以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimTurretFromWall : MonoBehaviour {
public Animator anim;
public GameObject triggerPanel;
public Transform player;
public int minDistanceFromTriggerPanel;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Vector3 distance = player.position - triggerPanel.transform.position;
if (distance.magnitude < minDistanceFromTriggerPanel)
{
anim.Play("TurretFromWall");
}
}
}
for the Transform player
, I'm using the FPS Controller from Standard Assets. 对于Transform player
,我正在使用Standard Assets的FPS Controller。 The triggerPanel
is the invisible GameObject. triggerPanel
是不可见的triggerPanel
。
You could also just use a OnTriggerEnter() function to start your animation. 您也可以只使用OnTriggerEnter()函数开始动画。
void OnTriggerEnter (Collider other)
{
if(other.gameobject.tag == "Player")
{
// animation code
Debug.Log("animation started...");
}
}
From here, be sure to add a BoxCollider to your tile, and tick the Trigger option, then ensure that your Player has a Rigidbody attached to it, this is how the OnTriggerEnter() works. 在这里,请确保将BoxCollider添加到拼贴中,并勾选Trigger选项,然后确保Player附加了一个刚体,这就是OnTriggerEnter()的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.