C# 碰撞会触发动画

C# 碰撞会触发动画,c#,unity3d,C#,Unity3d,我试图让我的播放器击中一个对象,破坏该对象并触发一个动画,但我尝试的一切都会导致错误。我在c#相对来说比较新,所以答案可能很明显,但我需要帮助。我如何设置它,以便碰撞将导致对象消失,播放器播放动画?这是我目前正在尝试的脚本 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public cl

我试图让我的播放器击中一个对象,破坏该对象并触发一个动画,但我尝试的一切都会导致错误。我在c#相对来说比较新,所以答案可能很明显,但我需要帮助。我如何设置它,以便碰撞将导致对象消失,播放器播放动画?这是我目前正在尝试的脚本

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

public class succ : MonoBehaviour
{
    public float speed = .15f;
    public static float jumpSpeed = 170f;
    void Start()
    {
        GetComponent<ConstantForce2D>().enabled = false;
        GameObject.Find("goal");
    }

    public bool animation_bool;
    private object coll;
    private object other;

    void Update()
    {
        OnCollisionStay2D(Collision2D coll);
        {
            if (coll.gameObject.tag == "succ") ;
            {
                animation_bool = true;
                GetComponent<Animator>().SetBool("succ", animation_bool);
                GetComponent<ConstantForce2D>().enabled = true;
                Destroy(other.object);
            }
        }
    }

    private void Destroy(object gameObject)
    {
        throw new NotImplementedException();
    }

    private void OnCollisionStay2D(Collision2D collision2D, object coll)
    {
        throw new NotImplementedException();
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类成功:单一行为
{
公众浮子速度=.15f;
公共静态浮跳速度=170f;
void Start()
{
GetComponent().enabled=false;
GameObject.Find(“目标”);
}
公共场所动画;
私有对象coll;
私人物品或其他物品;
无效更新()
{
OnCollisionStay2D(碰撞2D coll);
{
如果(coll.gameObject.tag==“成功”);
{
动画布尔=真;
GetComponent().SetBool(“成功”,动画);
GetComponent().enabled=true;
销毁(其他物品);
}
}
}
私有无效销毁(对象游戏对象)
{
抛出新的NotImplementedException();
}
专用空心OnCollisionStay2D(碰撞2D碰撞2D,对象coll)
{
抛出新的NotImplementedException();
}
}

我可以看出有几件事是错误的,但我将首先回答你的问题

我建议您将您的单行为方法OnCollisionStay2D更改为OnCollisionInter2D。是“在另一个对象上的碰撞器与该对象的碰撞器接触的每个帧发送”。是“当传入碰撞器与该对象的碰撞器接触时发送”

我相信你是在寻找后者,因为你只想在碰撞中触发一次。您还破坏了另一个对象,使得调用OnCollisionStay2D不再可能,即使您想这样做

您还应该删除更新方法。老实说,我不明白你现在想在那里实现什么。所有迎面而来的方法都会被自动调用;你不必自己给他们打电话

然后可以使用Awake和onCollisionInter2D方法,如下所示

public class Succ : MonoBehaviour
{
    private Animator animator;

    private void Awake()
    {
        // You can already get a reference to the Animator on Awake
        // This way you do not have to do it on every collision
        animator = GetComponent<Animator>();
    }

    // Use OnCollisionEnter2D instead since the code
    // needs to be excecuted only once during the collision
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("succ")
        {
            // Assuming that you only want to trigger an animation once
            // to reflect attacking or colliding, you could use SetTrigger
            // instead. Otherwise you need to use SetBool again to set it
            // back to false. You should then change the Animator parameter 
            // accordingly, from a bool to a trigger.
            animator.SetTrigger("succ");
            Destroy(collision.gameObject);
        }  
    }
}
公共类成功:单一行为
{
私人动画师;
私人空间
{
//您已经可以在唤醒时获得对动画师的引用
//这样,您就不必在每次碰撞时都这样做
animator=GetComponent();
}
//请改用OnCollisionInter2D,因为代码
//在碰撞过程中只需执行一次
专用空心OnCollisionInter2D(碰撞2D碰撞)
{
if(collision.gameObject.CompareTag(“成功”)
{
//假设您只想触发一次动画
//要反映攻击或碰撞,可以使用SetTrigger
//相反,否则需要再次使用SetBool进行设置
//返回false。然后应更改Animator参数
//因此,从bool到trigger。
设置触发器(“成功”);
破坏(碰撞,游戏对象);
}  
}
}
除此之外,我还有几件事想谈一谈:

  • 我不确定您试图通过在开始时将ConstantForce2D组件设置为false,然后在碰撞时将其设置为true来实现什么
  • 您似乎正在使用GameObject.Find on Start.GameObject.Find是一种应该很少使用的功能。它可能非常昂贵,尤其是当您的场景中有很多游戏对象时;这是因为它只需通过层次结构,将参数字符串与游戏对象的名称进行比较,直到找到匹配项或运行ou大量的游戏对象
  • 此外,您正在使用GameObject.Find开始查找GameObject,但是您不会将其存储在任何地方,这使得整个查找过程毫无意义

总的来说,我建议您看看Unity自己提供的所有不同的学习资源。您的问题是关于在所有不同的教程中肯定会涉及到的相当基本的功能。

在看到
Update
函数中的
OnCollisionStay2D
回调函数后,我建议你首先学习C#。有很多在线教程。这将节省你的时间,同时也节省其他人阅读你的问题的时间。