小编Pro*_*mer的帖子

如何使光线投射忽略触发器对撞机?

我有一个函数,检查游戏对象是否可以看到另一个游戏对象,而不会阻止其视图:

public bool CheckVision(GameObject target)
{
    RaycastHit2D ray = (Physics2D.Raycast(transform.position, target.transform.position - transform.position, m_VisionRange));
    if(ray.collider.name == target.name)
    {
        Debug.DrawRay(transform.position, target.transform.position - transform.position);
        return true;               
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一的问题是我不希望它与设置了"isTrigger"标志的碰撞器碰撞; 我希望raycast能够忽略它们.有任何想法吗?

c# unity-game-engine raycasting unity2d

1
推荐指数
2
解决办法
3249
查看次数

从C++本机插件更新float数组

在尝试将数组从C++传递给C#时,我看到了一个非常奇怪的问题.我正在使用Marshal.Copy(具体来说:https://msdn.microsoft.com/en-us/library/a53bd6cz(v = vs1010 ).aspx ).

问题:从C++到C#的float数组NaN在结果数组中产生了一些. (注意:我在Unity游戏引擎的上下文中工作)


示例C++代码:

extern "C" bool UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API getSomeFloats(float** points, int* count) {
        std::vector<float> results;
        std::vector<SOME_TYPE> key_points = <SOME_POINTS>

        for (auto iter = key_points.begin(); iter < key_points.end(); iter++) {
            results.push_back(static_cast<float>(iter->pt.x));
            results.push_back(static_cast<float>(iter->pt.y));
        }

        *points = results.data();
        *count = results.size();

        //<Print results to csv here>

        return true;
}
Run Code Online (Sandbox Code Playgroud)

示例C#代码:

[DllImport("NativePlugin")]
private static extern bool getSomeFloats (ref IntPtr ptrResultItems, ref int resultItemsLength);

private static float[] getFloatArrayFromNative() {
        IntPtr ptrResultItems = IntPtr.Zero;
        int …
Run Code Online (Sandbox Code Playgroud)

c# c++ interop unity-game-engine

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

减慢刚体速度

我添加了基于 的前向力GetAxis ("Vertical")和高达 的速度调节器normal max speed

我还允许“速度提升”,在正常附加力的基础上增加更多的力,最高可达boosted max speed

我遇到的困难是一旦升压期结束就减慢物体的速度

因此,基本上,如果您将速度提升到比正常最大速度更快的速度,并且您无法再进行速度提升,请将物体减慢回正常最大速度。

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

    float velocity;
    float magnitude = myRigidBody.velocity.magnitude;
    float vertical = Input.GetAxis ("Vertical");
    if (Input.GetKeyUp(KeyCode.LeftShift)) {
        boost = false;
    }
    if (Input.GetKeyDown (KeyCode.LeftShift)) {
        boost = true;
    }
    if (!boost) {
        velocity = vertical * speed;
        Vector3 force = myRigidBody.transform.forward * velocity;
        if (magnitude + force.sqrMagnitude < normalMaxSpeed) { 
            myRigidBody.drag = 0f;
            myRigidBody.AddForce (force, ForceMode.Impulse);
        } else if (magnitude + force.sqrMagnitude >= maxTrust + …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

更改启动屏幕/启动画面颜色和图像

该应用程序本身完全正常.我的问题是,在启动应用程序本身时,屏幕是Unity默认蓝色,大约一小时左右才变为我的第一个场景的黑色.

如何在启动时将此颜色更改为黑色?

c# unity-game-engine

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

如何在Unity3D中检查智能手机是否有陀螺仪?

我正在使用Google Cardboard SDK。在旧版本的Google Cardboard SDK 中,它包含一个名为AndroidManifest.xml的文件

<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true"/>
Run Code Online (Sandbox Code Playgroud)

因此,解决方案可能是将该文件简单地添加回我的项目。

但是我不喜欢它,因为如果我这样做,如果设备没有陀螺仪,该应用程序就会隐藏在 Google Play 中,这让用户感到困惑,因为他们倾向于在 Google Play 中搜索它。

那么,我如何从代码中检查设备是否有陀螺仪以向用户显示一条消息,例如:这款智能手机确实有陀螺仪。请尝试使用其他智能手机。

c# unity-game-engine google-cardboard google-vr google-vr-sdk

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

循环通过Transform的子时出错

我想遍历转换的所有子项,但我收到错误.

这是我想让所有孩子来自的变量:

public Transform parentToSearch;
Run Code Online (Sandbox Code Playgroud)

然后我在编辑器中将一个Transform对象从Hierarchy拖到了脚本中parentToSearch.

然后在脚本中我想循环遍历此Transform的所有子项:

private void OnGUI()
    {
        if (hasDescription == true && clickForDescription == true)
        {
            foreach (GameObject child in parentToSearch)
            {
                if (child.GetComponent<ItemInformation>() != null)
                {
                    ItemInformation iteminformation = child.GetComponent<ItemInformation>();
                    if (child.name == objectHit)
                    {
                        var centeredStyle = GUI.skin.GetStyle("Label");
                        centeredStyle.alignment = TextAnchor.UpperCenter;
                        GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), iteminformation.description, centeredStyle);
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

例外是在线:

foreach (GameObject child in parentToSearch)
Run Code Online (Sandbox Code Playgroud)

这是错误:

InvalidCastException:无法从源类型转换为目标类型

c# unity-game-engine unity5

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

附着在GameObject上的粒子系统为粉红色

我正在研究统一粒子系统。

我创建了一个新项目,然后创建了一个空对象,然后向其中添加了粒子系统。由于某种原因,它无法正常工作,您可以看到附件中的图像,以查看发生了什么...

在此处输入图片说明

c# 3d game-development unity-game-engine

1
推荐指数
2
解决办法
1580
查看次数

在 Android 上播放大视频无延迟

我正在开发 AR 游戏。在特定情况下,我想在统一的场景的一部分(例如在飞机上)播放用户从手机图库中选择的视频。我测试了统一视频播放器,但是当视频大小超过 100 MB 时会延迟很多,甚至纹理也无法显示,只是我可以听到视频声音。

我现在该怎么办?我应该编写一个Java本机插件来在Java中流式传输视频并统一设置纹理吗?

感谢和抱歉英语不好。

    videoPlayer.playOnAwake = false;
    videoPlayer.source = VideoSource.Url;
    videoPlayer.url = url;
    videoPlayer.Prepare();

    //Wait until video is prepared
    while (!videoPlayer.isPrepared)
    {
        Debug.Log("Preparing Video");
        yield return null;
    }

    //Assign the Texture from Video to RawImage to be displayed
    image.texture = videoPlayer.texture;

    //Play Video
    videoPlayer.Play();
Run Code Online (Sandbox Code Playgroud)

我还在编辑器中分配了 AudioSource。只要视频大小低于 10 MB,一切都正常。

c# android video-streaming unity-game-engine vuforia

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

在销毁GameObjects时我应该取消字段吗?

如果通过Inspector设置了一个字段,我应该在销毁GameObject时使其无效吗?Unity会自动执行此操作吗?

public class TestClass : MonoBehaviour
{
    public Image Icon;
    public Button CloseButton;

    private void Start()
    {
        Icon.color = Color.black;
        CloseButton.onClick.AddListener( MyButtonListener );
    }

    private void OnDestroy()
    {
        CloseButton.onClick.RemoveListener( MyButtonListener );

        //DO I NEED THIS?
        Icon = null;
        CloseButton = null;
    }

    private void MyButtonListener() { }
}
Run Code Online (Sandbox Code Playgroud)

c# memory-management unity-game-engine

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

MissingReferenceException:"Attacker"类型的对象已被破坏,但您仍在尝试访问它

我收到上述错误,我尝试使用debug.log打印错误的位置.我正在创造一种坦克obj.哪个实例化了obj.实例化对象是攻击者.

在更新中,我使用foreach循环遍历所有实例化对象.如果找到并且If对象在射程范围内.

void Update () {


        if (attacker!= null  )
        {
            //Debug.Log("inside att");
            attacker = FindObjectsOfType<Attacker>();
        }

        // fire only if attacker is in range 

        if (IfInRange() && attacker!= null && running)
        {

            faceTowardTarget();
            Fire();
        }


    }
    bool IfInRange()
    {// run through all the instantiated attacker
        foreach (Attacker currentAttacker in attacker)
Run Code Online (Sandbox Code Playgroud)

这工作正常,但有些时候会给出上面的结果.在控制台的最后,循环继续进行,currentAttacker最后为空.我试着在控制台中打印它.但它不会进入其他if语句

{   //if attacker is in range

            if (attacker != null )
                {

                    Debug.Log(currentAttacker.GetComponent<Health>().isAlive);

                    if (Vector2.Distance(transform.position, currentAttacker.transform.position) < minDistance)               
                    {

                        attackerPos = currentAttacker.transform;

                        return true;
                    }
                }
                if (currentAttacker …
Run Code Online (Sandbox Code Playgroud)

c# 2d unity-game-engine

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