2017-11-07 2 views
0

Dies wurde ungefähr 1 Million Mal abgefragt, aber ich kann keine Lösung für mein Problem finden, eher zu meinem Ärger.Der Typ `UnityEngine.Rigidbody 'kann nicht über eine integrierte Konvertierung in` ProjectileController' umgewandelt werden

Hilfe ist sehr geschätzt, da ich nicht sicher bin, was schief geht und wie man es mehr löst.

Script 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. 
} 

Die folgende von dem previos aufgerufen wird, wenn instansiated, verursacht Fehler wird dies aufgrund der Art zu schaffen, oder das, was ich versuche? Script 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

Sie müssen zumindest angeben, in welcher Zeile der Fehler auftritt. Wie nebenbei, Sie haben "Bullet" falsch buchstabiert (Ihres hat 3 Ls) –

+0

Wie sieht die Unterschrift der Instantiate aus? Sucht es auf der ersten Stelle nach einem Starrkörper? Es scheint auch, dass es eine Rigidbody zurückgibt und wenn ja, gibt es eine Konvertierung für Rigidbody zu ProjectileController (wie Sie versuchen, es über "wie" zu konvertieren)? Es ist möglicherweise nicht bewusst, wie diese Konvertierung durchgeführt wird. – Robert

+0

@ChrisDunaway Zeile 49, oder die Zeile enthält "ProjileController newProjectile = Instantiate (cannonballInstance, transform.position, transform.rotation) als ProjectileController; 'ignorieren Rechtschreibung ist nur ein Prototyp gerade jetzt – Robertgold

Antwort

1

Die Fertig (cannonballInstance) Sie instanziieren als Rigidbody deklariert. Wenn Sie die Instantiate Funktion aufrufen und cannonballInstance an es übergeben, gibt es eine Rigidbody nicht ProjectileController zurück.

ProjectileController ist ein Skript. Sie können nicht die zurückgegebene Rigidbody zu ProjectileController werfen. Sie müssen GetComponent verwenden, um die ProjectileController Instanz abzurufen, die an Ihr Prefab angeschlossen ist (cannonballInstance).

Es ist besser, diese Codezeile in Stücke zu zerlegen, um das Debuggen zu erleichtern, für den Fall, dass alles null ist.

Rigidbody rg = Instantiate(cannonballInstance, transform.position, transform.rotation); 
ProjectileController newProjectile = rg.GetComponent<ProjectileController>(); 
Verwandte Themen