我正在尝试制作一个2D平台生成器,但我的玩家没有向两边开火,我不知道我的脚本出了什么问题。
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
public float bulletSpeed;
public GameObject bullet;
public Transform bulletX;
GameObject clone;
void Update ()
{
if (Input.GetKeyUp ("space"))
{
clone = Instantiate(bullet,new Vector3(bulletX.position.x,bulletX.position.y+0.1f,0f),Quaternion.identity) as GameObject;
if (GameObject.Find ("Player").GetComponent<Player> ().left == true)
bulletSpeed = -30f;
else
bulletSpeed = 30f;
}
bullet.rigidbody2D.velocity = new Vector2(bulletSpeed * 0.5f, 0f);
Destroy (clone, 1f);
}
}
我试着在if条件下提高速度,但子弹的移动速度比我需要的要快。
我认为你的问题很难理解你想要实现什么,但我可以在你的代码中看到一些错误,这些错误会使你正在创建的"克隆"变得毫无用处。
更新循环不断执行,您已将destroy放置在"Press space"代码块之外。团结试图摧毁它的每一帧。把它放在空间里。
我觉得它应该看起来更像这样:
if (Input.GetKeyUp ("space"))
{
clone = Instantiate(bullet,new Vector3(bulletX.position.x,bulletX.position.y+0.1f,0f),Quaternion.identity) as GameObject;
if (GameObject.Find ("Player").GetComponent<Player> ().left == true)
bulletSpeed = -30f;
else
bulletSpeed = 30f;
bullet.rigidbody2D.velocity = new Vector2(bulletSpeed * 0.5f, 0f);
Destroy (clone, 1f);
}
这可能无法回答你的问题,但你能详细说明你追求的是什么行为吗?这个脚本运行在哪个对象上?(它被称为Bullet
,字段引用另一个bullet
?)
调试。记录您的bulletSpeed*0.5f
如果我是对的。即使你的球员正朝另一个方向看。它正在回归一个ABS数字,意思是(bulletSpeed=-30f和0.f是!=44954,但=44954)。
或者,您可以使用AddForce而不是Velocity。它更容易控制。
问题。为什么不使用实例化矢量2?