小编Pro*_*mer的帖子

碰撞后立即停止刚体运动/旋转

我希望我的球体从一个位置跳到另一个位置,但不希望它随后平移。我不知道该怎么做。这是我的代码:

void Update()
{
    if (!thrown && ((Input.touchCount > 0 
    && Input.GetTouch(0).phase == TouchPhase.Ended) 
    || Input.GetMouseButtonDown(0)))
    {
        rb.isKinematic = false;
        rb.AddForce(new Vector3(0.0f, 15.0f, 5.0f) );
        thrown = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

来回旋转游戏对象

我想在 Y 轴上在 90,-90 之间来回旋转对象。问题是我可以将编辑器中的对象设置为 -90,但是当我运行项目时 -90 突然变为 270。无论如何这里是我正在使用的代码:

void Update()
{
    if (transform.eulerAngles.y >= 270)
    {
        transform.Rotate(Vector3.up * speed * Time.deltaTime);
    }
    else if (transform.eulerAngles.y <= 90)
    {
        transform.Rotate(Vector3.up * -speed * Time.deltaTime);

    }
}
Run Code Online (Sandbox Code Playgroud)

它总是卡在 360 度左右的中间。帮助?

c# rotation euler-angles unity-game-engine

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

正确的方法来移动刚体GameObject

我刚刚开始学习Unity。我试图通过使用此脚本来移动一个简单的盒子。前提是,只要有人按下“ w”,盒子就会向前移动。

public class PlayerMover : MonoBehaviour {

public float speed;
private Rigidbody rb;


public void Start () {
    rb = GetComponent<Rigidbody>();
}

public void Update () {
    bool w = Input.GetButton("w");

    if (w) {
        Vector3 move = new Vector3(0, 0, 1) * speed;
        rb.MovePosition(move);
        Debug.Log("Moved using w key");

    }

}
}
Run Code Online (Sandbox Code Playgroud)

每当我使用此按钮时,该框都不会在“ w”按键上向前移动。我的代码有什么问题?我以为可能是我设置Vector 3move的方式,所以我尝试用速度替换z轴,但这没有用。有人可以告诉我我在搞砸吗?

c# unity-game-engine unity5

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

读取屏幕点的像素颜色(高效)

我需要在屏幕点读取 C# Unity3D 中的像素颜色。

我正在使用渲染纹理和 ReadPixels 方法。每 0.5f 秒使用一次对性能不利(即使我的渲染纹理大小为 128x128px),我正在尝试找到另一种方法来更快地获取此数据。我在某处看到可以直接使用,glReadPixels但我不知道如何使用它?

我需要检测这个地方(屏幕上的点)是否有东西。

这是我现在拥有的代码。

using UnityEngine;
using System;
using System.Collections;

public class ColController : MonoBehaviour
{
    public Camera collCamera;
    public Texture2D tex2d;


    void Start()
    {
        collCamera = GetComponent<Camera>();
        tex2d = new Texture2D(collCamera.targetTexture.width, collCamera.targetTexture.height, TextureFormat.ARGB32, false);

        RenderTexture.active = collCamera.targetTexture;
        StartCoroutine (Execute ());

    }

    IEnumerator Execute()
    {
        while (true) {

                yield return new WaitForEndOfFrame ();
                tex2d = GetRTPixels (collCamera.targetTexture);
                yield return new WaitForSeconds (0.5f);

        }
    }


    static public Texture2D GetRTPixels(RenderTexture rt)
    { …
Run Code Online (Sandbox Code Playgroud)

c# c++ android unity-game-engine

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

在单个脚本中同时具有IEnumerator Start()和void Start()

我有一个来自Unity文档页面的示例程序,其中包含IEnumerator Start()如下所示,但我想知道如何void Start()在同一个脚本中也能正常运行?

我也尝试添加void Start()它,但它引发了一个错误.然后,我试着在一个IEnumerator函数中包含我的代码(它只是写入控制应用程序的数据路径),虽然通过使用0f延迟参数立即执行它,但它不会打印出任何东西......

我错过了什么?这种情况的常见解决方案是什么,你必须有一个IEnumerator Start()但你还需要执行启动代码?

/// Saves screenshots as PNG files.
public class PNGers : MonoBehaviour
{

    // Take a shot immediately.
    IEnumerator Start()
    {
        yield return UploadPNG();
        yield return ConsoleMSG();
    }

    IEnumerator UploadPNG()
    {
        // We should only read the screen buffer after frame rendering is complete.
        yield return new WaitForEndOfFrame();

        // Create a texture the size of the screen, with RGB24 format.
        int width …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

Unity输入在印刷机中多次检测到

我有一个MonoBehavior使用这些方法的源脚本:

private void Update () {
    if (Input.GetMouseButton(0)) {
        HandleInput();
    }
}

private void HandleInput () {
    Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(inputRay, out hit)) {
        var position = transform.InverseTransformPoint(hit.point);
        Coordinates coords = Coordinates.FromPosition(position);
        HexCell cell = null;        
        if (!cells.TryGetValue(coords, out cell)) {
            var err = string.Format("{0} is an invalid position and coordinates are improperly calculated: {1}", position, coords);
            print(err);

            throw new System.ArgumentException(err);
        }

        print(string.Format("[{0}] cell: [{1}] {2}", DateTime.Now, cell.id, cell.coordinates));
        OnCellTouched(cell);
    }
}
Run Code Online (Sandbox Code Playgroud)

网格是透明的,我按预期得到六边形(单元格).在编辑器中,当我单击一个单元格时,我得到一条消息输出:

[05.31.2017.14.15.14.2836] …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

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

按名称,标签或图层查找非活动的GameObject

首先,我需要停用一个游戏对象,然后在10秒后激活它,所以我认为协同程序是合适的:

IEnumerator BarDeactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = GameObject.Find("OBJ");
        obj.SetActive(false);
}

IEnumerator BarReactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = transform.Find("OBJ");
        obj.SetActive(true);
}
Run Code Online (Sandbox Code Playgroud)

显然,我不能再使用,GameObject.Find所以我用它transform.Find来找到不活跃的游戏对象,但当然.SetActive现在不行,因为obj实际上并不是游戏对象...

如何投射找到的变换,以便可以再次设置它?

我试过obj.gameObject.SetActive(true)但它一定是错的,因为这个物体不会复活......

c# unity-game-engine

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

按下键时,在两种颜色之间切换

我正在学习C#,Unity我创建了一个简单的游戏,您可以通过单击"C"键(将变为绿色)更改播放器的颜色(默认情况下为红色).代码可以工作,但问题是,我不知道如何创建一个代码,通过使用相同的键("C")再次将绿色更改为红色.我知道的唯一选择是创建另一个if但使用不同的按钮.这是我的代码:

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

public class cubecolor : MonoBehaviour {


    // Use this for initialization
    void Start () {
        gameObject.GetComponent<Renderer> ().material.color = Color.red;

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.C))
            gameObject.GetComponent<Renderer> ().material.color = Color.green;

    }
}
Run Code Online (Sandbox Code Playgroud)

c# colors unity-game-engine

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

将 Texture2D 转换为 OpenCV Mat?

有一篇关于将 OpenCV 转换为Unity的帖子,我提供了一个效果很好的答案。现在,我正试图做相反的事情,但现在已经坚持了几个小时。cv::MatTexture2D

我想将 Unity 转换Texture2D为 OpenCV,cv::Mat以便我可以在 C++ 端处理纹理。

这是Texture2D我要转换为 Unity 项目中的原始文件cv:Mat

在此处输入图片说明

这是转换成后的样子cv:Mat

在此处输入图片说明

它看起来很褪色。我不担心图像的旋转。我可以解决这个问题。只是想知道为什么它看起来如此褪色。也用于cv::imwrite保存图像以进行测试,但问题也出现在保存的图像中。

C# 代码

[DllImport("TextureConverter")]
private static extern float TextureToCVMat(IntPtr texData, int width, int height);

unsafe void TextureToCVMat(Texture2D texData)
{
    Color32[] texDataColor = texData.GetPixels32();
    //Pin Memory
    fixed (Color32* p = texDataColor)
    {
        TextureToCVMat((IntPtr)p, texData.width, texData.height);
    }
}

public Texture2D tex;

void Start()
{
    TextureToCVMat(tex);
}
Run Code Online (Sandbox Code Playgroud)

C++ 代码

DLLExport …
Run Code Online (Sandbox Code Playgroud)

c# c++ opencv unity-game-engine

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

Unity 按钮不起作用/突出显示/交互

我已经编写了程序,一旦关卡完成,就会弹出一个面板并附加这个按钮。然而这个按钮不起作用。

我已经 - 检查了我的事件系统,检查了光线投射设置,设置了 onclick 事件,还将按钮放在 z 轴上,这样它就不会受到面板的干扰。

我的层次结构

在此输入图像描述

按钮的检查器

在此输入图像描述

画布检查员

在此输入图像描述

c# button unity-game-engine

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

标签 统计

c# ×10

unity-game-engine ×10

c++ ×2

android ×1

button ×1

colors ×1

euler-angles ×1

opencv ×1

rotation ×1

unity5 ×1