无法转换类型`UnityEngine.Rigidbody'到'ProjectileController'通过内置转换

时间:2017-11-07 19:49:46

标签: c# unity3d rigid-bodies

这已被问过大约一百万次,但我找不到解决我的问题的办法,而是我的烦恼。

非常感谢帮助,因为我不确定哪里出了问题以及如何解决它。

脚本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.
}

以下是从previos调用的,当发生时,导致错误,这是由于我试图创建的类型或什么? 脚本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

    }

}

}

1 个答案:

答案 0 :(得分:1)

您实例化的预制件(cannonballInstance)声明为Rigidbody。当您致电Instantiate函数并将cannonballInstance传递给它时,它将返回Rigidbody而不是ProjectileController

ProjectileController是一个脚本。您无法将返回的Rigidbody投射到ProjectileController。您必须使用GetComponent来检索附加到预制件ProjectileController)的cannonballInstance实例。

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

最好将这行代码分成几部分,以便在任何事情都为空时更容易调试。

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