小编mos*_*alf的帖子

有没有办法使用 Vector3 制作 switch/case?

我想做的是当我的角色(ThirdPerSonController)触摸立方体时检测每一面。事实上,我的主要目标是检测我的玩家何时站在顶部的立方体表面上。这是我的脚本:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    void OnCollisionEnter(Collision collision)
    {
        ContactPoint contact = collision.contacts[0];
        Vector3 normal = contact.normal;
        Vector3 inverseNormal = transform.InverseTransformDirection(normal);
        Vector3 roundNormal = RoundVector3(inverseNormal);

        ReturnSide(roundNormal);
    }

    Vector3 RoundVector3(Vector3 convertThis)
    {
        int x = (int)Mathf.Round(convertThis.x);
        int y = (int)Mathf.Round(convertThis.y);
        int z = (int)Mathf.Round(convertThis.z);
        Vector3 returnVector = new Vector3(x, y, z);
        return returnVector;
    }



void ReturnSide(Vector3 side)
{
    string output = null;
    switch (side)
    {
        case Vector3.down:
            output = "Down";
            break;
        case Vector3.up:
            output = …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

6
推荐指数
1
解决办法
8102
查看次数

播放并等待动画/动画师完成播放

在我的脚本中,当玩家位于平台顶部时,我就这样做了.它工作正常.但是现在我想让它一旦起来播放剪辑"Down".

using UnityEngine;
using System.Collections;
using System.Reflection;

public class DetectPlayer : MonoBehaviour {

    GameObject target;

    public void ClearLog()
    {
        var assembly = Assembly.GetAssembly(typeof(UnityEditor.ActiveEditorTracker));
        var type = assembly.GetType("UnityEditorInternal.LogEntries");
        var method = type.GetMethod("Clear");
        method.Invoke(new object(), null);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Platform")
        {
            Debug.Log("Touching Platform");
        }

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "OnTop Detector")
        {
            Debug.Log("On Top of Platform");
            GameObject findGo = GameObject.Find("ThirdPersonController");
            GameObject findGo1 = GameObject.Find("Elevator");
            findGo.transform.parent = findGo1.transform;
            target = GameObject.Find("Elevator");
            target.GetComponent<Animation>().Play("Up");
        }
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

5
推荐指数
1
解决办法
5838
查看次数

清除编辑器控制台记录脚本

这是我的脚本,我试图制作"",以防它不是其中一个案例:

using UnityEngine;
using System.Collections;

public class DetectPlayer : MonoBehaviour {

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Platform")
        {
            Debug.Log("Touching Platform");
        }
        else
        {
            Debug.Log("");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "OnTop Detector")
        {
            Debug.Log("On Top of Platform");
            GameObject findGo = GameObject.Find("ThirdPersonController");
            GameObject findGo1 = GameObject.Find("Elevator");
            findGo.transform.parent = findGo1.transform;
        }
        else
        {
            Debug.Log("");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这""不起作用.在游戏运行时,它不会从控制台中删除文本.而且当我停止游戏并再次运行它时,它会继续显示Debug.Log的最后一个文本.

如果在我的脚本中没有发生If,我想清除它.

我看到了这个答案:

回答

但我不确定这是否是我需要的以及如何使用它.制作一个新剧本?

还有错误呢.如果我清除登录控制台,它会删除erorrs,如果有一些?

在这个屏幕截图中,我触摸平台然后离开它后处于状态,但文本仍然存在于控制台日志中:

截图

c# unity-game-engine

4
推荐指数
1
解决办法
4656
查看次数

为什么团结一致我收到警告:您正在尝试使用'new'关键字创建MonoBehaviour?

完整的警告信息:

您正在尝试MonoBehaviour使用new关键字创建一个.这是不允许的.MonoBehaviours只能添加使用AddComponent().或者,您的脚本可以继承ScriptableObject或根本不继承基类

在这两个脚本中,我没有使用new关键字:

DetectPlayer脚本:

public class DetectPlayer : MonoBehaviour 
{
    private int counter = 0;

    private void OnGUI()
    {
        GUI.Box(new Rect(300, 300, 200, 20),
            "Times lift moved up and down " + counter);
    }
}
Run Code Online (Sandbox Code Playgroud)

Lift脚本:

public class Lift : MonoBehaviour 
{
    private bool pressedButton = false;
    private DetectPlayer dp = new DetectPlayer();

    private void OnGUI()
    {
        if (pressedButton)
            GUI.Box(new Rect(300, 300, 200, 20), …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

-4
推荐指数
1
解决办法
3863
查看次数

标签 统计

c# ×4

unity-game-engine ×4