私のコードの問題は、弾丸が基本的にプレーヤーオブジェクトからこぼれることですが、障害物にぶつかるために直接前方に発射させることはできません。代わりに、プレイヤーが各方向に転がると、弾丸は同じセットベクトルを出しますが、これは私が望むものではありません。弾丸はx軸に沿ってのみ発射するようにしたいのですが、代わりに、プレイヤーの転がり方に応じてすべての側面から発射します。
弾丸の力がプレイヤーの方向と勢いを変えた場合に備えて、弾丸の回転と力を変えてみました。それは失敗したので、私はテストするためにプレイヤーの前進力を取り除きましたが、それでも進歩はありません。
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float backwardForce = -60f;
public float forwardForce = 100f;
public float sidewaysForce = 1f;
// Start is called before the first frame update
void Start()
{
rb.AddForce(0, 100, 200);
}
// Update is called once per frame
void FixedUpdate()
{
//Movement force
rb.AddForce(0, 0, 50.0f * Time.deltaTime);
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else if (Input.GetKey("w"))
{
rb.AddForce(forwardForce * Time.deltaTime, 0, 0, ForceMode.Force);
}
else if (Input.GetKey("s"))
{
rb.AddForce(backwardForce * Time.deltaTime, 0, 0, ForceMode.Force);
}
/*switch (Input.GetKeyDown())
{
// case Input.GetKeyDown("a"):
//a: rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
}
}
public class BulletShoot : MonoBehaviour
{
//Reference to bullet emitter
public GameObject Bullet_Emitter;
//Reference to bullet prefab
public GameObject Bullet;
//Forward force of bullet
public float Bullet_Force;// = 10.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//When "Space" key is pressed..
if (Input.GetKey("space")){
//Temp variable that will hold reference to bullet
GameObject tempBulletHandler;
//Temp then instantiates bullet to game using emitter position and angle
tempBulletHandler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation);
//Corrects angle that bullets appear
tempBulletHandler.transform.Rotate(Vector3.left * 180);
//RigidBody instantiated to control bullet physics using bullet handler
Rigidbody tempRigidBody = tempBulletHandler.GetComponent<Rigidbody>();
//Add force to the rigidbody to simulate firing the bullet forward using set bullet force
tempRigidBody.AddForce(Vector3.forward * Bullet_Force);
//Free up space from bullets fired (after 4 sec)
Destroy(tempBulletHandler, 4.0f);
}
}
}
期待:プレイヤーは、直線方向に障害物を撃つ能力を持ちながら、継続的に前進します。
実際:プレイヤーは正しく動きますが、すべての方向に回転し続ける1つの穴のあるバッグから米が落ちるように、弾丸は制御できずに放出されます。
これは一般的な初期の問題です。弾丸、矢印、さらには何かからパーティクルをインスタンス化すると、割り当てられた変換がオブジェクトの親になります。したがって、そのインスタンス化がプレーヤーが持ち運ぶためのボックスである場合、それは問題ない可能性があります。親/プレーヤーが移動すると、ボックスも移動するためです。それをコーディングする必要はありません。
宇宙侵略者を想像してみてください。もしあなたが厄介なところで左から発射し、そして右端に向かいたいと思ったら、あなたが期待するのは、あなたが上にとどまるために送ったミサイル、その列の厄介なもの、現在何が起こるかですあなたが右に移動したときのあなたのミサイルであり、また右に移動します。ミサイルの親を外して、ミサイルが陽気な方向に進み、出発した方向に自由に移動できるようにする必要があります。
つまり、missle.transform.SetParent(newParent);を設定します。ここで、newparentはnullにすることもでき、階層の最上位に配置されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加