using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody rBody;
public float speed;
Ray ray;
RaycastHit hit;
bool isAttacking = false;
bool mouseOnPlayer = false;
public float playerSpeed = 50f;
public float hAxis, vAxis;
playerHealth pHealth;
public float test;
Animator anim;
void Start()
{
pHealth = GetComponent<playerHealth> ();
anim = GetComponent<Animator> ();
rBody = GetComponent<Rigidbody> ();
}
void Update()
{
if (pHealth.getHealth () > 0) {
isAttacking = anim.GetBool ("Attack");
if (!isAttacking)
Move ();
TurnToMouse ();
} else
anim.SetTrigger ("IsDead");
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rBody.AddForce (movement*speed);
}
void Move()
{
vAxis = Input.GetAxis ("Vertical");
hAxis = Input.GetAxis ("Horizontal");
if (vAxis!=0||hAxis!=0)
{
anim.SetBool ("Run", true);
anim.SetBool ("Idle",false);
}
if (vAxis == 0 && hAxis == 0)
{
anim.SetBool ("Idle",true);
anim.SetBool ("Run", false);
}
Vector3 movement = new Vector3 (hAxis, 0, vAxis) * playerSpeed;
rBody.AddRelativeForce (movement);
}
void TurnToMouse(){
if (!mouseOnPlayer) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
Vector3 fixedLook = new Vector3 (hit.point.x, test, hit.point.z);
this.transform.LookAt (fixedLook);
}
}
}
void OnMouseEnter(){
mouseOnPlayer = true;
}
void OnMouseExit(){
mouseOnPlayer = false;
}
}