약 1 백만 번도 요청되었지만 문제점에 대한 해결책을 찾지 못했습니다.내장형 변환을 통해`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.
}
제 1, 오류가 내가 만들려고하거나 무엇을하고있어이 때문에 유형되는 원인 instansiated 때 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
}
}
}
오류가 발생한 행을 적어도 알려야합니다. 제쳐두고, 당신은'Bullet' 철자가 정확하지 않습니다 (당신의 것은 3 L입니다) –
Instantiate의 서명은 어떻게 생겼습니까? 첫 번째 자리에서 리지드 바디를 찾고 있습니까? 또한 Rigidbody를 반환하고 Rigidbody를 ProjectileController로 변환하는 것으로 나타납니다 ('as'를 통해 변환하려고 함)? 전환을 만드는 방법을 알지 못할 수도 있습니다. – Robert
@ChrisDunaway Line 49, 또는 그 라인에 'ProjectileController newProjectile = Instantiate (cannonballInstance, transform.position, transform.rotation)가 ProjectileController로 포함되어 있습니다. '맞춤법 무시는 지금 바로 프로토 타입 일뿐입니다. – Robertgold