2017-12-07 22 views
1

화합에 약간의 문제가 있습니다. 나는 발사와 이동 시스템을위한 두 개의 다른 빌드를 가지고 있습니다. 이제는이 작업을 수행 할 때 몇 가지 문제가 발생하지만 함께 포팅 할 것입니다. 자산 폴더에있는 '총알'프리 패브는 게임 장면에 스폰되어야하지만 결코 그렇지 않습니다. 그러나 더 흥미로운 점은 '총알'과 스크립트의 다른 기능이 발생한다는 것입니다. 따라서 장면에 표시되지 않는 것은 조립식 일뿐입니다.Unity Prefab이 게임 장면으로 인스턴스화하지 않습니다.

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; 

} 
} 

모두가 두 개의 별도 버전에서 동일하지만 작동하지 않습니다 조립식의에서 그냥 산란 다음

조립식를 instaniates 스크립트에서 코드입니다.

모든 아이디어/제안을 주시면 감사하겠습니다. 코드에서

답변

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