【问题标题】:Instantiating and random Quaternions实例化和随机四元数
【发布时间】: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通用 并且无论如何都会返回给定参数的类型,所以如果BulletGameObject,它无论如何都会返回GameObject .. 这里不需要强制转换
  • 还要注意一般attackPoint.transform.rotation.z会返回意想不到的结果!四元数不仅有x,y,z,还有w,它的所有值都只在-11之间=>很可能不是你想要的值......

标签: unity3d


【解决方案1】:
    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);

您正在将 Instantiate 函数的四元数参数转换为 GamObject。删除“as GameObject”,它应该可以工作。

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 2019-07-12
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2011-04-18
    相关资源
    最近更新 更多