[英]Unity 3D, how to spawn objects, and modify their values?
我尝试制作一个简单的 3D 轨道射手,我目前坚持如何为我的船生成子弹。
我希望当我按下火时,子弹会产生并被射击。 我创建了一个空的游戏对象作为我船的子对象,并在其上放置了一个脚本。
问题是我目前卡住了,我不知道如何完成它。 所以我寻求帮助,我错过了什么,我做错了什么?
这是我想出的脚本:
public Rigidbody rb;
private bool isMoving = false;
private bool isFirePressed = false;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
}
void Update()
{
isFirePressed = Input.GetButtonDown("Fire1");
}
void FixedUpdate()
{
if (Input.GetButtonDown("Fire1"))
{
// the cube is going to move upwards in 10 units per second
rb.velocity = new Vector3(0, 0, 100);
isMoving = true;
Debug.Log("fire");
}
}
首先,我猜你想使用你的变量isFirePressed
。
然后,如果那是一个预制件,我猜你宁愿想Instantiate
一个新的子弹:
if (isFirePressed)
{
var newBullet = Instantiate (rb, transform);
// the cube is going to move upwards in 10 units per second
newBullet.velocity = new Vector3(0, 0, 100);
newBullet.useGravity = false;
isMoving = true;
Debug.Log("fire");
}
您更改了不起作用的预制件上的速度。
另外请注意, velocity
位于世界空间坐标中。 因此,无论您的飞机面向何处,目前您都在世界 Z 方向拍摄。
我宁愿使用例如
newBullet.velocity = transform.forward * 100;
朝 BulletEmitter 所面对的方向射击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.