在对撞机输入上播放动画师 [英] Play animator on collider enter

查看:770
本文介绍了在对撞机输入上播放动画师的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个对我有用的代码.除了动画之外,一切正常.

I have wrote a code that semi-works for me. Everything works aside from working animation.

我想做的是当Player进入触发器时,如果他/她按E,它将执行动画. (顺便说一句,动画工作正常,所以只是代码错误?).

What I'm trying to do is that when Player enters trigger, and if he/she presses E it will perform an animation. (Animation is working fine btw, so it's just code error?).

这是我到目前为止所得到的:

Here's what I got so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class barTrigger : MonoBehaviour {

    // press shit to drink text
    public GameObject drinkText;

    // bottle
    public GameObject alcBottle;

    // drink animator
    public GameObject animatorOjbect;
    Animator drinkAmin;

    public bool triggerIsOn;

    // Use this for initialization
    void Start () {

        drinkAmin = animatorOjbect.GetComponent<Animator> ();

        drinkText.SetActive(false);
        alcBottle.SetActive(false);

    }

    // Update is called once per frame
    void Update () {

    }

    void OnTriggerEnter(Collider other){

        triggerIsOn = true;

        if (other.gameObject.name == "vThirdPersonController") {
            drinkText.SetActive (true);

        }

        if (triggerIsOn && Input.GetKeyDown (KeyCode.E)) {

            drinkAmin.Play ("Dab");
            alcBottle.SetActive (true);
        }


    }

    void OnTriggerExit(Collider other){

        if (other.gameObject.name == "vThirdPersonController") {
            drinkText.SetActive (false);
            alcBottle.SetActive (false);
        }
    }
}

推荐答案

OnTriggerEnter被调用一次,很难同时按E按钮和Input.GetKeyDown(KeyCode.E)进行检测.为此,检查 OnTriggerStay 函数中的E输入更为合适,但是OnTriggerStay有时不起作用,因此请数一数.虽然值得知道它的存在.

OnTriggerEnter is called once and it will be hard to get press the E button and get Input.GetKeyDown(KeyCode.E) to detect that at the-same time. Checking the E input in the OnTriggerStay function is more appropriate for this but OnTriggerStay does not work sometimes so count it out. Although it's worth knowing that it exist.

Input.GetKeyDown(KeyCode.E)移动到称为每帧Update函数,然后在OnTriggerEnter函数中将其设置为true,在OnTriggerExit函数中将其设置为false.以下是应更改的代码.

Move Input.GetKeyDown(KeyCode.E) to the Update function which is called every frame then set it to true in the OnTriggerEnter function and false in the OnTriggerExit function. Below are the code that should be changed.

void Update()
{
    if (triggerIsOn && Input.GetKeyDown(KeyCode.E))
    {
        Debug.Log("E button pressed");
        drinkAmin.Play("Dab");
        alcBottle.SetActive(true);
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "vThirdPersonController")
    {
        Debug.Log("Detected vThirdPersonController");
        triggerIsOn = true;
        drinkText.SetActive(true);
    }
}

void OnTriggerExit(Collider other)
{

    if (other.gameObject.name == "vThirdPersonController")
    {
        Debug.Log("Lost vThirdPersonController");
        triggerIsOn = false;

        drinkText.SetActive(false);
        alcBottle.SetActive(false);
    }
}

一旦工作,就开始使用CompareTag函数而不是gameObject.name,因为那样会更熟练.

Once you get that working, starting using the CompareTag function instead of gameObject.name as that is more proficient.

这篇关于在对撞机输入上播放动画师的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆