2017-12-07 2 views
1

1つの問題で少し問題があります。私は発射と移動システムのための2つの異なるビルドを持っていましたが、私はこれを一緒に移植していますが、私はこれを行う際にいくつかの問題に遭遇します。アセットフォルダ内の「弾丸」プレハブは、ゲームシーンに生まれるように求められますが、そうではありません。しかし、もっと興味深いのは、「弾丸」と脚本の他の機能が実行されることです。そのため、シーンには表示されないプレハブだけです。Unity Prefabはゲームシーンにインスタンス化しません

次はプレハブをinstaniatesスクリプトからのコードです:

public bool isFiring; // Boolean to check if the player is firing 
public bool isReloading = false; // Boolean to check if the player is reloading 

public BulletController bullet; // Reference another script 
public float bulletSpeed; // bullet speed - changed in bullet controller 

public float timeBetweenShots; // time between shots can be fired 
private float shotCounter; // Tempoary time holder - ensures no bullet spam 

public Transform firePoint; // The fire point in the game attached to the gun 

public static int ammoRemaining = 3; // Ammo left for the player to fire 

public static int maxAmmo = 3; 
public Text ammoText; 

public Rigidbody cannonballInstance; 
public BulletController projectile; 

[Range(10f, 80f)] 
public float angle = 45f; 

// Use this for initialization 
void Awake() { 
    isReloading = false; 
    timeBetweenShots = 0.3f; 
    ammoRemaining = maxAmmo; 
} 

// Update is called once per frame 
void Update() { 

    if (ammoRemaining == 0 && isReloading == false) 
    { 
     StartCoroutine(Reload()); 

    } 

    else if (isFiring == true && isReloading == false) 
    { 
     shotCounter -= Time.deltaTime; 
     if(shotCounter <= 0 && ammoRemaining > 0 && isReloading == false) 
     { 
      shotCounter = timeBetweenShots; 

      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 

      RaycastHit hitInfo; 
      if (Physics.Raycast(ray, out hitInfo)) 
      { 
       FireCannonAtPoint(hitInfo.point); 
      } 

      ammoRemaining -= 1; 
      ammoText.text = "Ammo:" + ammoRemaining; 

     } 


    } 
    else if (Input.GetKey(KeyCode.R)) 
    { 
     StartCoroutine(Reload()); 
    } 

    else 
    { 
     shotCounter = 0; 


    } 

} 

private void FireCannonAtPoint(Vector3 point) 
{ 
    Vector3 randomAccuracy; 

    randomAccuracy = new Vector3(Random.Range(-2.0f, 2f), 0, Random.Range(-2f, 2f)); 

    var velocity = BallisticVelocity(point + randomAccuracy, angle); 
    Debug.Log("Firing at " + (point + randomAccuracy) + " velocity " + velocity); 



    Rigidbody rg = Instantiate(cannonballInstance, transform.position, transform.rotation); 
    Debug.Log("Firing at" + transform.position); 
    Debug.Log(rg.transform); 
    BulletController newProjectile = rg.GetComponent<BulletController>(); 

    newProjectile.speed = velocity; 
    Debug.Log(newProjectile.speed); 

    // cannonballInstance.transform.position = transform.position ; 
    // cannonballInstance.velocity = velocity; 
} 

private Vector3 BallisticVelocity(Vector3 destination, float angle) 
{ 
    Vector3 direction = destination - transform.position; // get Target Direction 
    float height = direction.y; // get height difference 
    direction.y = 0; // retain only the horizontal difference 
    float distance = direction.magnitude; // get horizontal direction 
    float AngleRadians = angle * Mathf.Deg2Rad; // Convert angle to radians 
    direction.y = distance * Mathf.Tan(AngleRadians); // set direction to the elevation angle. 
    distance += height/Mathf.Tan(AngleRadians); // Correction for small height differences 

    // Calculate the velocity magnitude 
    float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude/Mathf.Sin(2 * AngleRadians)); 
    Debug.Log(velocity); 
    return velocity * direction.normalized; // Return a normalized vector. 

} 



public IEnumerator Reload() 
{ 
    isReloading = true; 
    ammoText.text = "REL..."; 

    yield return new WaitForSeconds(2); 
    ammoRemaining = maxAmmo; 
    isReloading = false; 
    ammoText.text = "Ammo:" + ammoRemaining; 

} 
} 

すべてが2つの別々のバージョンでは同じですが、それは動作しませんプレハブの中だけで産卵。

ご意見やご提案は大変ありがとうございます。

コードから

答えて

1

:ないの問題であっても変更して

Rigidbody rg = Instantiate(cannonballInstance.gameObject, transform.position, transform.rotation).GetComponent<Rigidbody>(); 
+0

:あなたは私はあなたがしてみてくださいゲームオブジェクト

をインスタンス化することを期待だと思う剛体のインスタンス化されている

public Rigidbody cannonballInstance; [...] Rigidbody rg = Instantiate(cannonballInstance, transform.position, transform.rotation); 

産卵はまだ存在する。あたかもゲームの階層に追加されていないかのように反動することができます。 – Robertgold

関連する問題