using UnityEngine; using System.Collections; public class MPBarbarbarainMovement : MonoBehaviour { public Vector3 currPosition; public GameObject cube; Vector3 distance; public bool move; public float speed; public float turnSpeed; public float minDistance; public float attackRange; public float damage; int state = 0; public bool grounded = false; public GameObject barMove; public GameObject barAttack; float timer = 0.0F; public float attackRate; public float jumpSpeed; public GameObject bloodSplatter; public AudioClip hitSound; public AudioClip swordSwingSound; public GameObject[] players; public GameObject attackOrb; private GameObject orb; public float orbH; public bool jumping; public float jumpTimer; public float attackChance; // Use this for initialization void Start () { if(networkView.isMine) orb = (GameObject)Instantiate(attackOrb,transform.position,new Quaternion(0,180,0,0)); } void FixedUpdate() { if(move && grounded && networkView.isMine) { rigidbody.velocity = transform.forward * speed; } } // Update is called once per frame void Update () { if(networkView.isMine) { if (Input.GetButton("Fire2")) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast (ray,out hit)) { // Create a particle if hit currPosition = hit.point; } if(grounded) turnBarbarian(); }else { barMove.animation.CrossFade("Standing", 0.2F); move = false; } //test highlightGoblin HighlightPlayer(); //increment timer timer += Time.deltaTime * 1; //find out how many goblins there are in the world players = GameObject.FindGameObjectsWithTag("Player"); //set the destination point then rotate towards it //if the user is not holding right mouse button then play //standing animationa and stop moving //if the user presses left mouse button play //attack animation rotate towards target //and apply damage to goblins if (Input.GetButton("Fire1")) { barAttack.animation.CrossFade("run", 0.2F); RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast (ray,out hit)) { // Create a particle if hit currPosition = hit.point; if(hit.transform.gameObject.tag == "Player" && !hit.transform.networkView.isMine) { if(Vector3.Distance(hit.point, transform.position) < attackRange) { if(timer > attackRate) { float randomTemp; Random.seed = (int)(Time.time + transform.position.x + transform.position.z); randomTemp = Random.Range(1, 100); if(randomTemp > attackChance) { AudioSource.PlayClipAtPoint(swordSwingSound, transform.position); AudioSource.PlayClipAtPoint(hitSound, hit.transform.position); Instantiate(bloodSplatter,hit.transform.position,new Quaternion(0,180,0,0)); //send damage to the object clicked on within the players attack range. hit.transform.networkView.RPC( "ApplyDamage", RPCMode.AllBuffered, damage); timer = 0; } } } } } //if the player isn't moving then rotate towards point //otherwise use animated running towards target if(!move) turnBarbarianAttack(); else turnBarbarian(); }else { //if the player isn't attacking tell the torso to //play idle animation barAttack.animation.CrossFade("idle",0.2F); } } } void HighlightPlayer() { if(networkView.isMine) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Physics.Raycast (ray,out hit)) { // Create a particle if hit currPosition = hit.point; if(hit.transform.gameObject.tag == "Player" && Vector3.Distance(hit.point , transform.position) < attackRange && !hit.transform.networkView.isMine) { orb.renderer.enabled = true; orb.transform.position = hit.transform.position + new Vector3(0,orbH,0); }else { orb.renderer.enabled = false; } } } } void turnBarbarian() { //move towards where the player clicked if( Vector3.Distance( currPosition, transform.position ) > minDistance && grounded && networkView.isMine ) { //if the user presses SpaceBar jump barMove.animation.CrossFade("RunAim", 0.2F); move = true; Quaternion targetRotation = Quaternion.LookRotation (currPosition - cube.transform.position , Vector3.up); cube.transform.rotation = Quaternion.Slerp(cube.transform.rotation, targetRotation, Time.deltaTime * turnSpeed); cube.transform.rotation = Quaternion.Euler(new Vector3(0f, cube.transform.rotation.eulerAngles.y, 0f)); }else { move = false; } } void turnBarbarianAttack() { if(grounded && networkView.isMine) { barMove.animation.CrossFade("Standing", 0.2F); Quaternion targetRotation = Quaternion.LookRotation (currPosition - cube.transform.position , Vector3.up); cube.transform.rotation = Quaternion.Slerp(cube.transform.rotation, targetRotation, Time.deltaTime * turnSpeed); cube.transform.rotation = Quaternion.Euler(new Vector3(0f, cube.transform.rotation.eulerAngles.y, 0f)); } } // This part detects whether or not the object is grounded and stores it in a variable void OnCollisionEnter (Collision walkable) { if(walkable.transform.gameObject.tag == "ground" && networkView.isMine) { state ++; if(state > 0) { grounded = true; } } } void OnCollisionExit (Collision walkable) { if(walkable.transform.gameObject.tag == "ground" && networkView.isMine) { state --; if(state < 1) { grounded = false; state = 0; } } } }