2017-02-26 4 views
0

弾丸がランダムな位置で発射されていて、実際にはカメラの前方に向かっているわけではありません。ここで何が間違っていて、どのように修正する必要がありますか? は、だから私は、プールを使用していますが、弾丸がこのコードを有効にするたびに実行されます。私のコードはなぜ弾丸をランダムな位置で発射するのですか?

private void OnEnable() 
    { 
     transform.position = Camera.main.transform.position; 
     transform.rotation =Quaternion.identity; 
     GetComponent<Rigidbody>().AddForce((Camera.main.transform.forward + new Vector3(0, 0, 0)) * 5000); 
     Invoke("Destroy", 1.5f); 
    } 

私はまた、以下のコードにそれを変更しましたが、でも、もう一つは動作しません。

private void OnEnable() 
    { 
     Rigidbody rb = GetComponent<Rigidbody>(); 
     rb.position = Camera.main.transform.position; 
     rb.rotation = Quaternion.identity; 
     rb.AddForce((Camera.main.transform.forward + new Vector3(0, 0, 0)) * 5000); 
     Invoke("Destroy", 1.5f); 
    } 

答えて

0

まず、コードがアウトプーリングで動作することを確認してください。次に、弾丸の衝突要素を無効にします(衝突する可能性があります)。

私はすぐに自分のマシンでこれを試しました。これが私の得た結果です。ここ

enter image description here

using UnityEngine; 

public class BulletController : MonoBehaviour 
{ 
    [SerializeField] 
    GameObject bulletPrefab; 

    void Update() 
    { 
     GameObject bullet = Object.Instantiate(bulletPrefab); 
     Rigidbody body = bullet.GetComponent<Rigidbody>(); 
     body.position = Camera.main.transform.position; 
     body.AddForce(Camera.main.transform.forward * 75f, ForceMode.Impulse); 
    } 
} 
+0

私は常に弾丸をカメラのまっすぐ前方に撃って、このようにはしたくありません。これをどのように修正すればよいですか? –

+0

@CrapshitJetluそれはまさにそれが何をしています。 – Iggy

0

Iの方向、位置および速度の変数を使用するコードです。

void Inception::shootGeode(std::string typeGeode, const btVector3& direction) { 

    logStderr(VERBOSE, "MESSAGE: Shooting geode(s)...\n"); 

    std::shared_ptr<Geode> geode; 

    glm::vec3 cameraPosition = m_camera->getPosition(); 
    btVector3 position = glm2bullet(cameraPosition + 3.0f * glm::normalize(m_camera->getTarget() - cameraPosition)); 

    if (typeGeode == "cube") 
     geode = m_objectFactory->createCube("cube", new btBoxShape(btVector3(1.0f, 1.0f, 1.0f)), position, "dice"); 

    if (typeGeode == "sphere") 
     geode = m_objectFactory->createSphere("sphere", new btBoxShape(btVector3(1.0f, 1.0f, 1.0f)), position); 

    btVector3 velocity = direction; 
    velocity.normalize(); 
    velocity *= 25.0f; 

    geode->getRigidBody()->setLinearVelocity(velocity); 
} 
関連する問題