Exemplo n.º 1
0
    /// <summary>
    /// This function is used to help set basic properties on a game object
    /// in a generic way.
    /// </summary>
    /// <param name="gameObject">The game object whose properties are to be set.</param>
    /// <param name="targetProperty">The target property to set.</param>
    /// <param name="propertyValue">The value to set the target property to.</param>
    ///
    public static void SetGameObjectProperty(GameObject gameObject, GameObjectProperty targetProperty, float propertyValue)
    {
        GameObject player = GameObject.Find("Player");
        ExponentialMovingAverage trailAvg = new ExponentialMovingAverage(0.06f);

        //check if the game object is null
        if (gameObject == null)
        {
            return;
        }

        //set the property
        switch (targetProperty)
        {
        case GameObjectProperty.XPosition:
            if (gameObject.transform != null)
            {
                gameObject.transform.position = new Vector3(propertyValue,
                                                            gameObject.transform.position.y,
                                                            gameObject.transform.position.z);
            }
            break;

        case GameObjectProperty.YPosition:
            if (gameObject.transform != null)
            {
                gameObject.transform.position = new Vector3(gameObject.transform.position.x,
                                                            propertyValue,
                                                            gameObject.transform.position.z);
            }
            break;

        case GameObjectProperty.ZPosition:
            if (gameObject.transform != null)
            {
                gameObject.transform.position = new Vector3(gameObject.transform.position.x,
                                                            gameObject.transform.position.y,
                                                            propertyValue);
            }
            break;

        case GameObjectProperty.XRotation:
            if (gameObject.transform != null)
            {
                gameObject.transform.rotation = Quaternion.Euler(new Vector3(propertyValue,
                                                                             gameObject.transform.rotation.eulerAngles.y,
                                                                             gameObject.transform.rotation.eulerAngles.z));
            }
            break;

        case GameObjectProperty.YRotation:
            if (gameObject.transform != null)
            {
                gameObject.transform.rotation = Quaternion.Euler(new Vector3(gameObject.transform.rotation.eulerAngles.x,
                                                                             propertyValue,
                                                                             gameObject.transform.rotation.eulerAngles.z));
            }
            break;

        case GameObjectProperty.ZRotation:
            if (gameObject.transform != null)
            {
                gameObject.transform.rotation = Quaternion.Euler(new Vector3(gameObject.transform.rotation.eulerAngles.x,
                                                                             gameObject.transform.rotation.eulerAngles.y,
                                                                             propertyValue));
            }
            break;

        case GameObjectProperty.XVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().velocity = new Vector3(propertyValue,
                                                                             gameObject.GetComponent <Rigidbody>().velocity.y,
                                                                             gameObject.GetComponent <Rigidbody>().velocity.z);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Translate(propertyValue * Time.deltaTime, 0.0f, 0.0f);
            }
            break;

        case GameObjectProperty.YVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().velocity = new Vector3(gameObject.GetComponent <Rigidbody>().velocity.x,
                                                                             propertyValue,
                                                                             gameObject.GetComponent <Rigidbody>().velocity.z);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Translate(0.0f, propertyValue * Time.deltaTime, 0.0f);
            }
            break;

        case GameObjectProperty.ZVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().velocity = new Vector3(gameObject.GetComponent <Rigidbody>().velocity.x,
                                                                             gameObject.GetComponent <Rigidbody>().velocity.y,
                                                                             propertyValue);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Translate(0.0f, 0.0f, propertyValue * Time.deltaTime);
            }
            break;

        case GameObjectProperty.XAngularVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().angularVelocity = new Vector3(propertyValue,
                                                                                    gameObject.GetComponent <Rigidbody>().angularVelocity.y,
                                                                                    gameObject.GetComponent <Rigidbody>().angularVelocity.z);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Rotate(propertyValue * Time.deltaTime, 0.0f, 0.0f);
            }
            break;

        case GameObjectProperty.YAngularVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().angularVelocity = new Vector3(gameObject.GetComponent <Rigidbody>().angularVelocity.x,
                                                                                    propertyValue,
                                                                                    gameObject.GetComponent <Rigidbody>().angularVelocity.z);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Rotate(0.0f, propertyValue * Time.deltaTime, 0.0f);
            }
            break;

        case GameObjectProperty.ZAngularVelocity:
            if (gameObject.GetComponent <Rigidbody>() != null)
            {
                gameObject.GetComponent <Rigidbody>().angularVelocity = new Vector3(gameObject.GetComponent <Rigidbody>().angularVelocity.x,
                                                                                    gameObject.GetComponent <Rigidbody>().angularVelocity.y,
                                                                                    propertyValue);
            }
            else if (gameObject.transform != null)
            {
                gameObject.transform.Rotate(0.0f, 0.0f, propertyValue * Time.deltaTime);
            }
            break;

        case GameObjectProperty.UniformScale:
            if (gameObject.transform)
            {
                gameObject.transform.localScale = new Vector3(propertyValue,
                                                              propertyValue,
                                                              propertyValue);
            }
            break;

        case GameObjectProperty.XScale:
            if (gameObject.transform)
            {
                gameObject.transform.localScale = new Vector3(propertyValue,
                                                              gameObject.transform.localScale.y,
                                                              gameObject.transform.localScale.z);
            }
            break;

        case GameObjectProperty.YScale:
            if (gameObject.transform)
            {
                gameObject.transform.localScale = new Vector3(gameObject.transform.localScale.x,
                                                              propertyValue,
                                                              gameObject.transform.localScale.z);
            }
            break;

        case GameObjectProperty.ZScale:
            if (gameObject.transform)
            {
                gameObject.transform.localScale = new Vector3(gameObject.transform.localScale.x,
                                                              gameObject.transform.localScale.y,
                                                              propertyValue);
            }
            break;

        case GameObjectProperty.YParentOffset1:
            if (gameObject.transform)
            {
                float smoothpVal;
                smoothpVal = trailAvg.average(propertyValue);
                gameObject.transform.position = new Vector3(player.transform.position.x,
                                                            player.transform.position.y + smoothpVal,
                                                            0f);
            }
            break;

        case GameObjectProperty.YParentOffset2:
            if (gameObject.transform)
            {
                float smoothpVal;
                smoothpVal = trailAvg.average(propertyValue);
                gameObject.transform.position = new Vector3(player.transform.position.x,
                                                            player.transform.position.y - smoothpVal,
                                                            0f);
            }
            break;

        case GameObjectProperty.TrailWidth:
            if (gameObject.transform)
            {
                gameObject.transform.GetComponent <TrailRenderer>().startWidth = Mathf.Pow(propertyValue, 2);
            }
            break;

        default:
            break;
        }
    }