当GameObject碰到触发器时,是否在Unity控制台中打印消息? Unity5/2D [英] Print message in Unity console when a GameObject hits a trigger? Unity5/2D

查看:154
本文介绍了当GameObject碰到触发器时,是否在Unity控制台中打印消息? Unity5/2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校制作游戏",我需要统计选票.我正在团结一致,让角色跳上按钮来计算票数,然后继续下一个问题.这是我到目前为止的内容:

I'm working on a "Game" for school, and I need to tally votes. I'm using unity to have characters jump on buttons to count the votes and move on to the next question. This is what I have so far:

using UnityEngine;
using System.Collections;

public class Vote1 : MonoBehaviour {

    private int vote1;
    public GameObject hero_1;

    void Start () {
        vote1 = 0;
    }

    void Update () {
    }
    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "PlayerObject"){
            vote1 = vote1 + 1;
            print("One Vote Added");
        }
    }
}

当按钮"hero_1"站立时,此应该加+1,并在控制台中显示消息已添加一个投票",但事实并非如此.我在做什么错了?

This should be adding +1 when "hero_1" stands on the button, and displaying the message "One Vote Added" in the console, but that's not what's happening. What am I doing wrong?

更新了代码

现在使用OnCollisionEnter2D,但仍然无法正常工作.我还想念什么?

now uses OnCollisionEnter2D, and still doesn't work as intended. What am I missing still?

推荐答案

由于printMonoBehaviour中定义的用于打印到Unity控制台的方法,因此我假设您实际上是在查看输出的Unity控制台,没有任何显示.这意味着OnTriggerStay没有被调用,或者对撞机的根目录没有PlayerObject标记.

Since print is a method defined in MonoBehaviour to print to the Unity console, I'm going to assume that you are in fact looking in the Unity console for the output, and nothing is showing up. That means that either OnTriggerStay is not getting called, or the collider's root does not have the PlayerObject tag.

OnTriggerStay.确保投票GameObject上具有Vote1组件,并且其对撞机已选中是触发器".

OnTriggerStay is called when a 3D physics body is inside of a trigger. Make sure that the vote GameObject has the Vote1 component on it, and its collider has "Is Trigger" checked.

还请确保该对象具有物理主体和对撞机,而该对撞机不是是触发器.对象的根变换(即层次结构中没有任何父项的根变换)应具有PlayerObject标记.如果玩家在父容器中,则transform.root将获取该父容器,而不是玩家本身.

Also make sure that the object has a physics body and collider which is not a trigger. The object's root transform (i.e. the one in the hierarchy without any parents) should have the PlayerObject tag. If the player is inside of a parent container, then transform.root will get that parent rather than the player itself.

此外,请确保未通过编辑"->项目设置"->物理"->层碰撞矩阵"过滤掉这两项之间的冲突.

Also, make sure that collisions between the two items are not being filtered out via Edit->Project Settings->Physics->Layer Collision Matrix.

最后,请确保已启用所有功能!

Last, make sure everything is enabled!

注意:您使用的是OnTriggerStay,当播放器处于触发状态时,每帧将被击中一次.如果您没有某种机制可以在投票后删除玩家,那么他们将每秒增加30-60票,具体取决于FPS.

Note: You are using OnTriggerStay which will be hit once per frame while the player is in the trigger. If you don't have some mechanism to remove the player once the vote is cast, then they will be adding 30-60 votes per second, depending on FPS.

这篇关于当GameObject碰到触发器时,是否在Unity控制台中打印消息? Unity5/2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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