Tuesday, January 26, 2016

Post 3

-We have to add a script to the enemy as well. We can call it EnemyAI, it looks like this:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

public GameObject m_player;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

Vector3 playerPos = m_player.GetComponent<Transform>().position;
Vector3 pos  = this.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = Vector3.MoveTowards(pos, playerPos, 0.05f);
}

}


}

-For starters we added a public GameObject m_player, this object will show up in the script component in unity, so go back to unity.
-Select the enemy and look for the script component there should be a text box labeled m_player.
-Now from the left panel press the left mouse button on Player and don't let it go so you can drag the Player to that text box labelled m_player, drop the Player on it and now the enemy script can use the Player object (this is called "having a reference" of the player object).
-We can get the position of the player by using m_player.

-For this script we needed to make the enemy follow the user.
-In this case we use a function already supplied by Vector3 called MoveTowards, we pass three pieces of data, the position of the player, the position of the enemy and a "speed" value and in return we get a new position for the enemy which makes the enemy get closer to the player

-Now if we run this in Unity we will notice that the Enemy chases the player with no pause, and because of the physics, the enemy and the player roll around.

-To stop the rolling we are going to lock the rotation in the RigidBody component
-Select the player
-In the left panel look for the RigidBody Component and check the x,y,z boxes for rotation.
-Do the same for the Enemy.
-If you run unity now you will see that the enemy and player do not roll anymore.

-Now for the issue with the user not getting a rest when the enemy reaches him, in most games when the player gets hit they get a chance to recover otherwise they will get hit constantly and die, which can be frustrating. So to give the player a change to recover we can give the enemy a cooldown time, here is the updated code:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

public GameObject m_player;
private float waitTime;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(waitTime >= 0)
{
waitTime -= Time.deltaTime;
//Debug.Log ("time " + waitTime);
}
else
{

Vector3 playerPos = m_player.GetComponent<Transform>().position;
Vector3 pos  = this.GetComponent<Transform>().position;
this.GetComponent<Transform>().position = Vector3.MoveTowards(pos, playerPos, 0.05f);
}

}

void OnTriggerEnter(Collider other)
{
waitTime = 0.5f;
m_player.GetComponent<PlayerController>().playerHit(this.GetComponent<Transform>().position);
}

}

- What we did here is add a waitTime variable that is private (so only the enemy can see it and use it)
- WaitTime is the cool down time, if this time is more than 0 then the enemy is cooling down, so we added a condition "if (waitTime >=0)" with this condition we make sure that if the waitTime is greater than 0 we take the elapsed time out of it (elapsed time can be found in the Time object its called deltaTime)
- If the time is 0 or less we will start moving the object (this is in the "else" area)

-OnTriggerEnter is a function called when something enters the area around and close to the enemy, if this happens we will update the wait time to be half a second so the enemy cools down for half a second and stays put.
-We also call a function in Player called playerHit, which will make the player bounce back when attacked. I will go into details on that in the next post.

-Now to get OnTriggerEnter to actually work we need to add a sphere collider to the enemy.
-Select Enemy and Add Component look for sphere collider and add it.
-In the components panel, in the sphere collider section there should be a "Is Trigger" check box, check it. Check in it activates the OnTriggerEnter Function in the EnemyAI script when another object with a rigid body enters it.

-We will have an error now since payerHit is not in Player yet.

No comments:

Post a Comment