我是Unity的全新人物 . 我在制作FPS射击游戏时遇到了困难 . 这是我在Ammo预制件中的代码:
public class Ammo : MonoBehaviour {
public GameObject obj ;
// Use this for initialization
void Start () {
obj= GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
transform.Translate (obj.transform.forward* Time.deltaTime);
}
void OnCollision(Collider coll)
{
if (coll.tag != "Player")
Destroy (gameObject);
}
}
在FPS中:
public class FPS : MonoBehaviour {
public GameObject Ammo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Instantiate(Ammo, transform.position,transform.rotation);
}
}
}
但是当我射击时,球体随机向 . (当我向前看时它会上升) . 有没有人帮助我 . 我是Unity的新手 .
1 回答
子弹不是随机方向 . 这取决于你的球员位置 . 首先,你应该从 players camera NOT FROM PLAYER中获取向量 . 因为你想拍摄你看的方向(因此相机) .