首页 > 解决方案 > 如何修复“无法将转换类型 'float' 隐式转换为 'UnityEngine.Quaternion'”?

问题描述

我一直有这个问题,无法弄清楚如何解决它。我正在制作一个 2D 游戏,需要一个旋转和收缩的六边形。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hexagon : MonoBehaviour
{
    public Rigidbody rb; 
    public float shrinkSpeed = 3f;

    // Start is called before the first frame update
    void Start()
    {
        rb.rotation = Random.Range(0f, 360f);
        transform.localScale = Vector3.one * 10f;
    }

    // Update is called once per frame
    void Update()
    {
        transform.localScale -= Vector3.one * shrinkSpeed * Time.deltaTime;

        if (transform.localScale.x <= .05f)
        {
            Destroy(gameObject);
        }
    }
}

我是编程新手,需要帮助。

标签: c#

解决方案


这可以工作

using System.Collections;

使用 System.Collections.Generic;使用 UnityEngine;

公共类 ob : MonoBehaviour { 公共刚体 rb;

public float shrinkspeed = 4f;

// Start is called before the first frame update
void Start()
{
    Quaternion randYRotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
    transform.rotation = randYRotation;
    transform.localScale = Vector3.one *10f;
}

// Update is called once per frame
void Update()
{
    transform.localScale -= Vector3.one * shrinkspeed * Time.deltaTime;
    
    if (transform.localScale.x <= .05f) 
    {
      Destroy(gameObject);
    }
}

}


推荐阅读