using UnityEngine; using System.Collections; public class GoblinMovement : MonoBehaviour { public Vector3 currPosition; public GameObject goblin; Vector3 distance; public float speed; public float turnSpeed; public float minDistance; public float attackRange; public GameObject target; public bool move; public bool grounded; public float attackRate; Quaternion targetRotation; public float damage; public float sightRange; float timer = 0.0F; int state = 0; public GameObject bloodsplatter; public AudioClip hitSound; public GameObject Legs; public float attackChance; private int difficulty; // Use this for initialization void Start () { difficulty = PlayerPrefs.GetInt("difficulty"); //switch the difficulty and based on the difficulty change the kill goals switch(difficulty) { case 0: //leave the settings as they are since they are so easy break; case 1: //change the attacking chances to 50/50 attackChance = 50.0F; break; case 2: //change the attacking change to 25 meaning 75% of the time they hit attackChance = 25.0F; break; } target = GameObject.Find("Barbarian"); } void FixedUpdate() { if(move && grounded) { Legs.animation.CrossFade("RelaxedWalk",0.2F); rigidbody.velocity = transform.forward * speed; } else rigidbody.velocity = -transform.up * speed; } // Update is called once per frame void Update () { if(target == null) return; // Create a particle if hit currPosition = target.transform.position; Vector3 rayDir = transform.TransformDirection(Vector3.forward); RaycastHit hitGob; //if there is a goblin in front of us we do not move if(Physics.Raycast (transform.position,rayDir,out hitGob, sightRange)) { if(hitGob.transform.gameObject.tag == "Goblin") { move = false; targetRotation = Quaternion.LookRotation(currPosition - transform.position ,Vector3.up); //rotate towards the target transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation , Time.deltaTime * turnSpeed); //make sure we only rotate on 1 axis and that is Y-Axis transform.rotation = Quaternion.Euler(new Vector3(0f,transform.rotation.eulerAngles.y, 0f)); } }else if( Vector3.Distance( currPosition, transform.position ) > minDistance && grounded ) { //flip the flag for movement to true move = true; goblin.animation.CrossFade("idle",1.2F); //if the player is further than 10 feet then add his velocity to //to where the goblin is looking so it can intercept other wise look //at the player if( Vector3.Distance( currPosition, transform.position ) > 10) { targetRotation = Quaternion.LookRotation (currPosition - transform.position + target.rigidbody.velocity , Vector3.up ); }else { //just look at the player since we are so close targetRotation = Quaternion.LookRotation (currPosition - transform.position , Vector3.up ); } //rotate towards the target transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation , Time.deltaTime * turnSpeed); //make sure we only rotate on 1 axis and that is Y-Axis transform.rotation = Quaternion.Euler(new Vector3(0f, transform.rotation.eulerAngles.y, 0f)); }else { move = false; targetRotation = Quaternion.LookRotation (currPosition - transform.position , Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * turnSpeed); transform.rotation = Quaternion.Euler(new Vector3(0f, transform.rotation.eulerAngles.y, 0f)); timer += Time.deltaTime * 1; if(timer > attackRate && Vector3.Distance( currPosition, transform.position ) < attackRange) { Legs.animation.CrossFade("Standing",2.2F); goblin.animation.CrossFade("run",2.2F); float randomTemp; Random.seed = (int)(Time.time + transform.position.x + transform.position.z); randomTemp = Random.Range(1, 100); if(randomTemp > attackChance) { target.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); AudioSource.PlayClipAtPoint(hitSound, target.transform.position); Instantiate(bloodsplatter,target.transform.position,new Quaternion(0,180,0,0)); } timer = 0.0F; } } } // This part detects whether or not the object is grounded and stores it in a variable void OnCollisionEnter ( Collision terrain) { if(terrain.gameObject.tag == "ground") { state ++; if(state > 0) { grounded = true; } } } //once we leave the ground start decreasing from the state //so we know we left the ground void OnCollisionExit (Collision terrain) { if(terrain.gameObject.tag == "ground") { state --; if(state < 1) { grounded = false; state = 0; } } } }