EDIT: So the problem is that all of my code is contained in IF statements, so when I click the mousebutton down it calls the movetopos function, which moves my player towards the transform. However I have no way of checking within that function when I have reached the range and then attacking. I have tried all types of while/do-while loops but cannot get it done.
All I want is Click left mouse button down → Retrieve enemies transform → move towards transform → when transform is reached to automatically attack.
I’ve gone over and condensed my code to show only necessary functions.
Thanks
void Update () {
InRange();
if (!die)
{
//Player clicks left mouse button to move only
if (Input.GetMouseButton(0))
{
//get position of where player clicked
GetPosition();
}
#region TEST UPDATE 1
//Player clicks the right mouse button, if enemyhit, player will move to enemy to attack, if not hit, enemy move to pos hit.
if (Input.GetMouseButtonDown(1)) //This button doubles as movement and attack
{
GetPosition();
if (!inRange && enemyHit)
{
if (inRange)
{
Attack();
}
}
else if (inRange && enemyHit)
{
_position = transform.position;
Attack();
}
}
// MoveToPos();
#endregion
}
//MoveToPos();
Impact();
if (!inRange)
{
MoveToPos();
}
if(inRange)
{
Attack();
}
}
void GetPosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f))
{
if (hit.collider.tag != "Player" && hit.collider.tag != "Enemy")
{
_position = new Vector3(hit.point.x, 0, hit.point.z);
enemyHit = false;
}
if (hit.collider.tag == "Enemy")
{
enemyHit = true;
opponent = opponent.gameObject;
if(!inRange)
_position = opponent.transform.position;
}
//Instantiate animatation to represent click pos on the terrain
//GameObject position = (GameObject)Instantiate(posClick, _position, Quaternion.identity);
}
}
void MoveToPos()
{
//GameObject is moving
if (Vector3.Distance(transform.position, _position) >= RADIUS)
{
GetMousePos();
Quaternion newRotation = Quaternion.LookRotation(_position - transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
//transform.position = Vector3.MoveTowards(transform.position, opponent.transform.position, step);
cc.SimpleMove(transform.forward * speed);
GetComponent<Animation>().CrossFade(run.name);
//GetComponent<Animation>().CrossFade(run.name);
}
//This was to check if play was within radius of attack, if they were it was meant to stop the player at their current position.
//else if (Vector3.Distance(transform.position, _position) <= RADIUS)
//{
// Attack();
//}
//Game object finished moving
else
{
GetComponent<Animation>().CrossFade(idle.name);
}
}
public void Attack()
{
//if (Input.GetMouseButton(1))
//{
// if (!started)
// {
// GetComponent<Animation>().Play(attackClip.name);
// attack = false;
// transform.LookAt(lookTarget);
// }
//}
if (!started && enemyHit)
{
transform.LookAt(lookTarget);
GetComponent<Animation>().Play(attackClip.name);
attack = false;
}
}