Example #1
0
    // Use this for initialization
    void Start()
    {
        //Assigns the player object
        player = GameObject.FindWithTag("Player");

        currentState = PiranhaPlantState.sleeping;
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        //The state of the Piranha when it has not detected the player
        if (currentState == PiranhaPlantState.sleeping)
        {
            piranhaPlantAnimator.SetBool("isIdling", true);
            if (Vector3.Distance(player.transform.position, this.transform.position) < distanceToWakeUp)
            {
                //Checks the speed of the player prevent the plant from responding to the player
                //if they're moving slowly enough

                CharacterController controller = player.GetComponent <CharacterController>();

                Vector3 playerVelocity = controller.velocity;
                float   playerSpeed    = playerVelocity.magnitude;

                if (playerSpeed > speedToWakeUp)
                {
                    Debug.Log("I'm awake!");

                    currentState = PiranhaPlantState.attacking;
                }
            }
        }

        //The state of the Piranha when it has not detected the player
        if (currentState == PiranhaPlantState.attacking)
        {
            piranhaPlantAnimator.SetBool("isAttacking", true);
            piranhaPlantAnimator.SetBool("isIdling", false);
            //Makes the plant to rotate to face the player whenthe player is in proximity to it
            Vector3 plantLookat = transform.position - player.transform.position;
            plantLookat.y = 0;


            piranhaPlantHead.transform.rotation = Quaternion.LookRotation(plantLookat, Vector3.up);

            //Quaternion dude = Quaternion.LookRotation (player.transform.position - transform.position, Vector3.up);
            //Vector3 dudeV3 = dude.eulerAngles;
            //piranhaPlantHead.transform.Rotate (new Vector3 (0f, dudeV3.y, 0f));

            //If the player gets far away enough, the plant will return to sleep
            if (Vector3.Distance(player.transform.position, this.transform.position) > distanceToSleep)
            {
                currentState = PiranhaPlantState.sleeping;
            }
        }
    }