2017-11-07 11 views
0

これは約100万回も尋ねられていますが、私の悩みの解決策は見つけられません。組み込み変換で `UnityEngine.Rigidbody 'を` ProjectileController'に変換できません

私は何が間違っているのか分かりませんし、もう解決する方法が分かりません。

スクリプト1:

public class Something : MonoBehaviour { 
[SerializeField] 
private Rigidbody cannonballInstance; 
public ProjectileController projectile; 
public Transform firePoint; 
[SerializeField] 
[Range(10f, 80f)] 
private float angle = 45f; 
private void Update() 
{ 
if (Input.GetMouseButtonDown(0)) 
{ 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    RaycastHit hitInfo; 
    if (Physics.Raycast(ray, out hitInfo)) 
    { 
     FireCannonAtPoint(hitInfo.point); 
    } 
} 
} 
private void FireCannonAtPoint(Vector3 point) 
{ 
var velocity = BallisticVelocity(point, angle); 
Debug.Log("Firing at " + point + " velocity " + velocity); 
ProjectileController newProjectile = Instantiate(cannonballInstance, transform.position, transform.rotation) as ProjectileController; 
//cannonballInstance.transform.position = transform.position; 
//cannonballInstance.velocity = velocity; 
} 
private Vector3 BallisticVelocity(Vector3 destination, float angle) 
{ 
Vector3 dir = destination - transform.position; // get Target Direction 
float height = dir.y; // get height difference 
dir.y = 0; // retain only the horizontal difference 
float dist = dir.magnitude; // get horizontal direction 
float a = angle * Mathf.Deg2Rad; // Convert angle to radians 
dir.y = dist * Mathf.Tan(a); // set dir to the elevation angle. 
dist += height/Mathf.Tan(a); // Correction for small height differences 
// Calculate the velocity magnitude 
float velocity = Mathf.Sqrt(dist * Physics.gravity.magnitude/Mathf.Sin(2 * a)); 
return velocity * dir.normalized; // Return a normalized vector. 
} 

instansiatedする場合は、次のは&過去から呼び出されたが、エラーが原因私が作成しようとしているタイプまたは何このある原因は? スクリプト2:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class ProjectileController : MonoBehaviour { 

public float speed; 
private Vector3 oldVelocity; 
private Rigidbody rigidbodyTemp; 
private int bounceLimit = 3; 

// Use this for initialization 
void Start() { 
    rigidbodyTemp = GetComponent<Rigidbody>(); 
    rigidbodyTemp.isKinematic = false; 
    rigidbodyTemp.freezeRotation = true; 
    rigidbodyTemp.detectCollisions = true; 
} 

// Update is called once per frame 
void FixedUpdate() { 
    rigidbodyTemp.AddForce(transform.forward * speed); 
    oldVelocity = rigidbodyTemp.velocity; 
} 

private void OnCollisionEnter(Collision collision) 
{ 
    bounceLimit -= 1; 
    if (collision.gameObject.tag == "Bulllet") // Check if hit another bullet 
    { 
     Destroy(this.gameObject); 
    } 
    else if (collision.gameObject.tag == "Crate") // Check if a Crate has been hit, will hold power ups 
    { 
     Destroy(this.gameObject); 
     Destroy(collision.gameObject); 
     PickUUpBounce.isActive = true; 
    } 

    else if (collision.gameObject.tag == "Player") // Check if hit a player 
    { 
     Destroy(this.gameObject); 
    } 
    else if (collision.gameObject.tag == "Enemy") // Check if enemy is hit 
    { 
     Destroy(this.gameObject); 
     Destroy(collision.gameObject); 
    } 

    else if (bounceLimit == 0) // check if bounce limit is reached 
    { 
     Destroy(this.gameObject); 
    } 
    else // bounce 
    { 
     Vector3 reflectedVelocity; 
     Quaternion rotation; 

     ContactPoint contact = collision.contacts[0]; // stores contact point for reflected velocity 

     reflectedVelocity = Vector3.Reflect(oldVelocity, contact.normal); // reflected velocity equals a reflection of the old velocity around the contact point 

     rigidbodyTemp.velocity = reflectedVelocity; // Change rigidbody velocity 
     rotation = Quaternion.FromToRotation(oldVelocity, reflectedVelocity); // old directyion -> new direction 
     transform.rotation = rotation * transform.rotation; // front face always facing the front 

    } 

} 

}

+0

エラーが発生した行を少なくとも特定する必要があります。とにかく、 'Bullet'は間違って綴られています(あなたのものは3Lです) –

+0

Instantiateの署名はどういうものですか?最初の場所でリジッドボディを探していますか?また、Rigidbodyを返すように見えます。もしそうなら、RigidbodyからProjectileControllerへの変換があります(asを使って変換しようとしています)?その変換を行う方法を認識していないかもしれません。 – Robert

+0

@ChrisDunaway Line 49、またはその行に 'ProjectileController newProjectile = Instantiate(cannonballInstance、transform.position、transform.rotation)がProjectileControllerとして含まれています。 'スペルを無視するのはただのプロトタイプです – Robertgold

答えて

1

プレハブ(cannonballInstance)あなたはRigidbodyとして宣言されてインスタンス化されています。 Instantiate関数を呼び出してcannonballInstanceを渡すと、RigidbodyではなくProjectileControllerが返されます。

ProjectileControllerはスクリプトです。 できません返されたRigidbodyProjectileControllerにキャストします。プレファブ(cannonballInstance)に添付されているProjectileControllerインスタンスを取得するには、GetComponentを使用する必要があります。

何かがnullの場合に備えて、デバッグが容易になるように、このコード行を分割してください。

Rigidbody rg = Instantiate(cannonballInstance, transform.position, transform.rotation); 
ProjectileController newProjectile = rg.GetComponent<ProjectileController>(); 
関連する問題