public PackageController SpawnRandomPackage()
    {
        PackageController package = Instantiate(packagePrefab, transform.position, transform.rotation);

        int          shapeEnumValue = Random.Range(0, System.Enum.GetNames(typeof(PackageShape)).Length);
        PackageShape shape          = (PackageShape)shapeEnumValue;

        package.SetShape(shape);

        int          colorEnumValue = Random.Range(0, System.Enum.GetNames(typeof(PackageColor)).Length);
        PackageColor color          = (PackageColor)colorEnumValue;

        package.SetColor(color);

        int           layoutEnumValue = Random.Range(0, System.Enum.GetNames(typeof(PackageLayout)).Length);
        PackageLayout layout          = (PackageLayout)layoutEnumValue;

        package.SetLayout(layout);

        Rigidbody rb             = package.GetComponent <Rigidbody>();
        float     randomAngle    = Random.Range(0.0f, 360.0f);
        float     randomStrength = Random.Range(0.0f, randomImpulseStrength);
        Vector3   force          = transform.rotation * Quaternion.AngleAxis(randomAngle, Vector3.up) * (Vector3.forward * randomStrength);
        Vector3   randomOffset   = new Vector3(Mathf.Sin(Random.Range(0.0f, Mathf.PI * 2.0f)), Mathf.Sin(Random.Range(0.0f, Mathf.PI * 2.0f)), Mathf.Sin(Random.Range(0.0f, Mathf.PI * 2.0f))) * 0.5f;

        rb.AddForceAtPosition(force, package.transform.position + randomOffset);

        if (OnPackageSpawned != null)
        {
            OnPackageSpawned(this, package);
        }

        return(package);
    }
Пример #2
0
    void GrabObject(PackageController _object)
    {
        if (m_grabbedObject != null)
        {
            Debug.LogError("Object already grabbed.");
            return;
        }

        m_grabbedObject          = _object;
        m_grabbedObjectRigidbody = m_grabbedObject.GetComponent <Rigidbody>();

        m_grabbedObjectRigidbody.useGravity = false;

        m_grabbedObjectUp       = grabZone.transform.InverseTransformDirection(m_grabbedObject.transform.up);
        m_grabbedObjectForward  = grabZone.transform.InverseTransformDirection(m_grabbedObject.transform.forward);
        m_grabbedObjectPosition = grabZone.transform.InverseTransformPoint(m_grabbedObject.transform.position);
    }