Click to move towards an opponents transform and attack when in range
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;
}
}
if (!InRange())
{
$$anonymous$$oveToPos();
//If player is in range they will attack only, not move
if (InRange())
{
Attack();
}
}
Your "Inrange" checks are within the same brackets. You first check "!Inrange" and when you know you're not in range you still say "if (inrange)". You also have a check in your $$anonymous$$ovePos-function, which says: if you're close enough, don't move. That means, if you are in range, "if(!Inrange)" will never be true. You will never move, and you will never check if you are in range. So as soon as you're in range the functions will never be called.
Atleast this is my first reaction, not sure if it actually would work anyway (since you do change your position before second check).
You can also try to change your check to: "Vector3.Distance(transform.position, _position) >= RADIUS" So that you make sure that you're actually within the radius and not exactly at the border.
Answer by GregIven · Nov 28, 2015 at 09:27 AM
@salmjak Ya I see what your saying, after looking into it a little more, I think the problem is that it checks upon each click whether I am in range or not. However, what I need it to do is upon each click: Check if in range, if not in range, I need to move to and then attack in the same click. If I am in range, just a simple attack.
I am stumped at the part of getting to move and attack in the same click....
Get$$anonymous$$ouseButton(0) is true when pressing and holding left mouse button :) So you might want just Get$$anonymous$$ouseButtonDown(0)? This will only be true when you click and will prevent the code from running with every update-tick.
Hey, so I updated the question in case it was a bit confusing. But I want a few things to happen when I click that button, I'll try what you suggested, but its not exactly my problem. Although it could help to smooth out the whole system.