【发布时间】:2021-05-23 10:13:08
【问题描述】:
void spawnBullet()
{
float speed = 250f;
// Spawns the bullet // Gets the point // Point a diffrent direction every time
GameObject Temporary_Bullet_Handler = Instantiate(Bullet, attackPoint.transform.position, Quaternion.Euler(Random.Range(2.557f, -2.557f), Random.Range(0f, -5.795f), attackPoint.transform.rotation.z) as GameObject);
// Makes the bullet work
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
// Shitty rigidy body
Rigidbody Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
// Adds the force for the bullet to move
Temporary_RigidBody.AddForce(transform.forward * speed);
// Kill the bullet
Destroy(Temporary_Bullet_Handler, 10.0f);
}
有人可以帮我实例化那行该死的代码吗?
我不断收到关于 cannot convert type unityengine.Quaternion to unityengine.GameObject 的错误,这让我生气了一个小时,我用谷歌(甚至去其他页面)尝试查找答案和我发现的只是关于vector3的废话
【问题讨论】:
-
这是一个错字:您关闭
Instatiate为时已晚)。因此,它尝试强制转换随机四元数as GameObject,这显然没有任何意义;)...你根本不需要它 ...Instantiate是通用 并且无论如何都会返回给定参数的类型,所以如果Bullet是GameObject,它无论如何都会返回GameObject.. 这里不需要强制转换 -
还要注意一般
attackPoint.transform.rotation.z会返回意想不到的结果!四元数不仅有x,y,z,还有w,它的所有值都只在-1和1之间=>很可能不是你想要的值......
标签: unity3d