我有一Component节课:
class Component
{
}
Run Code Online (Sandbox Code Playgroud)
然后是一个Collider继承自它的类:
class Collider :public Component
{
}
Run Code Online (Sandbox Code Playgroud)
并且SphereCollider继承自Collider:
class SphereCollider :public Collider
{
}
Run Code Online (Sandbox Code Playgroud)
最后,GameObject您可以使用添加或删除组件的类.
该GameObject.h标题:
class GameObject
{
//Add functionality
template <class T>
T& AddComponent();
//Get functionality
template <typename T>
T& GetComponent();
}
Run Code Online (Sandbox Code Playgroud)
该GameObject.cpp来源:
template <typename T>
T& GameObject::AddComponent(){}
template <typename T>
T& GameObject::GetComponent(){}
//Explicit instantiation
template Component &GameObject::GetComponent<Component>();
template Component &GameObject::AddComponent<Component>();
template SphereCollider &GameObject::GetComponent<SphereCollider>();
template SphereCollider &GameObject::AddComponent<SphereCollider>();
Run Code Online (Sandbox Code Playgroud)
一切都很好.我可以在没有指针的情况下从GameObject添加或获取组件,如下所示:
GameObject …Run Code Online (Sandbox Code Playgroud) 我什么都试过了。无论我在它下面放置什么样的游戏对象(立方体等),玩家都会失败。它有一个圆形煤矿和一个刚体。
我怎样才能阻止物体从地板上掉下来
我应该提一下,玩家一旦接触到任何东西就应该死,所以我不知道如何解决这个问题。
我是 Unity 的新手
使用 Unity Inspector 我设置了一个按钮来回调函数(OnClick),它工作正常,但只有一次,要再次触发该操作,我需要释放并再次单击该按钮
我怎样才能使该功能在按下按钮时不断运行?(像机关枪一样)
public void MoveLeft ( )
{
transform.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
infopanel.RotateAround(Camera.main.transform.position, Vector3.up, -rotation / 4 * Time.deltaTime);
}
Run Code Online (Sandbox Code Playgroud)
问候...
在Unity中使用协程时遇到了一个奇怪的问题。修改之前,我的代码如下:
IEnumerator Destory()
{
yield return new WaitForSeconds(destoryDelay);
yield return StartCoroutine(Timer.Start(0.5f, false, gameManager.EnableBtnSummon));
GameObject.Destroy(this.gameObject);
}
Run Code Online (Sandbox Code Playgroud)
Time.Start() 是我自己编写的实用程序,用于延迟调用。
public static IEnumerator Start(float duration, bool repeat, Action callback)
{
do
{
yield return new WaitForSeconds(duration);
if (callback != null)
callback();
} while (repeat);
}
Run Code Online (Sandbox Code Playgroud)
因为Time.Start()包含了WaitForSeconds(),所以我决定修改上面的代码如下:
IEnumerator Destory()
{
//yield return new WaitForSeconds(destoryDelay);
yield return StartCoroutine(Timer.Start(destoryDelay+0.5f, false, gameManager.EnableBtnSummon));
GameObject.Destroy(this.gameObject);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,控制台抛出错误:
ArgumentException:值不在预期范围内。
gameManager.EnableBtnSummon只是一个动作处理游戏逻辑。调试后,我确保该函数运行之前发生错误。但我将展示更多线索。
public void EnableBtnSummon()
{
//will not reach this!
print("Enable Button Summon");
//if detecting monster, change …Run Code Online (Sandbox Code Playgroud) 我统一制作了一个测试游戏,当我单击按钮时,它会生成一个从工厂类创建的圆柱体。我试图做到这一点,以便当我创建圆柱体时,它的高度会在接下来的 20 秒内缩小。我发现的一些方法很难转化为我正在做的事情。如果您能引导我走向正确的方向,我将非常感激。
这是我的圆柱体类代码
public class Cylinder : Shape
{
public Cylinder()
{
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(3, 0, 0);
cylinder.transform.localScale = new Vector3(1.0f, Random.Range(1, 2)-1*Time.deltaTime, 1.0f);
cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
Destroy(cylinder, 30.0f);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在做一个滚球比赛,球在那里移动,当球与立方体碰撞时它会计数.每次球移动时我都需要对比赛进行计数,但我似乎无法弄明白.我一直在收到错误.我感谢任何帮助.使用UnityEngine;
// Include the namespace required to use Unity UI
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
// Create public variables for player speed, and for the Text UI game objects
public float speed;
public Text countText;
public Text winText;
public Text movesText;
// Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far
private Rigidbody rb;
private int count;
private int moves;
private float location;
// At …Run Code Online (Sandbox Code Playgroud) 在 Unity 中,如何引用 SpaceBar 键以便在按下空格键时对对象施加力?
void Update()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
if (Input.GetKey("a"))
{
rb.AddForce(500 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("w"))
{
rb.AddForce(0, 0, -500 * Time.deltaTime);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, 500 * Time.deltaTime);
}
if (Input.GetKey("d"))
{
rb.AddForce(-500 * Time.deltaTime, 0, 0);
}
if (Input.GetKey("vbKeySpace"))
{
rb.AddForce(0, 300 * Time.deltaTime, 0);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在制作一个ARCore应用程序(应该没关系,不应该吗?),在放置之前,您可以在屏幕中央看到可见的模型.用户可以更改这些模型.
我想要获得的效果是在你将它设置为"幻影"/半透明之前获得你所看到的模型.我理想的是拥有一个空的GameObject(现在名为GhostWrapper),它是Ghost的父级,并且GhostWrapper中的所有内容都是半透明的.这是因为Ghost模型可以随用户输入而改变,然后当用户想要放置Ghost时,Ghost会在飞机上实例化:
public void SetPlaceableModel(GameObject newModel)
{
PlaceableModel = newModel;
var pos = (Ghost != null) ? Ghost.transform.position : FirstPersonCamera.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height / 2f, -100f));
var rot = (Ghost != null) ? Ghost.transform.rotation : Quaternion.identity;
Destroy(Ghost);
Ghost = Instantiate(PlaceableModel, pos, rot, GhostWrapper.transform);
}
Run Code Online (Sandbox Code Playgroud)
我无法让GhostWrapper工作,所以下面是我试图直接对Ghost做的事情,但我更愿意,如果我可以拥有,实质上,GhostWrapper导致其中的所有内容看起来都是半透明的(它不必强迫它的孩子透明,但让它们看起来像它).
我已经谷歌了很多,并尝试了顶级结果中建议的方式,但几乎所有似乎都是"获得MeshRenderer并设置.material.color为透明"的变体,这似乎并没有起作用.我也尝试在我的模型上设置材质以使用不同的着色器并将颜色设置为alpha,但这些都没有起作用(我看到的所有答案似乎都来自2009-2014,所以也许他们"刚刚过时了吗?" 我也试过Unity文档所说的无济于事.
我想过让GhostWrapper成为一个真正的盒子模型,它包含Ghost并操纵它以使内部的项目显得透明,但由于模型的形状/大小可以大不相同,并且用户可以靠近它们移动,可能只是在盒子内移动,它会失去效果.
谁能帮我这个?
注意:我正在使用的模型有Unit/Texture着色器.我很乐意改变它,如果我可以让它工作,但我已经改为其他几个,包括使用Standard和更改Render Modeto Fade和Transparent.仍然没有运气.
我拼命地寻找到解决该问题的方法,但还没有找到任何可能的办法,也许您可以帮帮我:我正在做一个简单的测试,其中一种圆柱体可以自行旋转并通过摩擦在平坦的平台上移动。我有刚体,已经附加了网格对撞机和控制旋转的脚本。它可以快慢移动,取决于其转速。脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float rollingSpeed;
public float gravity;
// Update is called once per frame
void FixedUpdate()
{
transform.Rotate(rollingSpeed, 0.0f, 0.0f);
GetComponent<Rigidbody>().AddForce(0.0f, -gravity, 0.0f);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我运行“播放”模式时,此圆柱体本身正在旋转,但保持在相同位置!(实际上,它来回移动的幅度很小),我真的不知道是什么原因,我试图增加摩擦参数,添加物理材料,甚至向下施加第二个力(如重力),但没有用。有人可以为此提供解决方案吗?非常感谢你们!
如何在Unity 2018.2+中为Android设备开发的程序中获取C#的IP地址?
似乎Network.player.ipAddress现在已经弃用了,所以我正在寻找一种新的方式.