How to make orb walking?
First I have to apologize if you confuse with my word. My problem is I want to create the game that control by mouse (like diablo or path of exile). Now I can make click to walk and attack but the animation is not like I really want.
I want to control like if you click to attack but you click the floor to walk away too fast and attack's animation is not reach the limit I want then that attack is invalid.
It's like the MOBA game that you can attack and then walk to cancel ' after attack's animation ' (sorry I don't know how you call it).
Edit : I know its name. It calls " orb walking " .
I use two script to deal with this.
The First one
using UnityEngine;
using System.Collections;
public class Phol_DamageBuffDebuff : MonoBehaviour {
GameObject targetTWO;
Animator anim;
public bool isAttackCD = false;
float PlayerDamage = 5f;
void Start ()
{
anim = GetComponent<Animator>();
}
public void Attack(GameObject target)
{
if(!isAttackCD)
{
targetTWO = target;
anim.SetInteger("PholParameter", 2);
isAttackCD = true;
Invoke("Damage", 0.6f);
Invoke("animationReset", 1.6f);
Invoke("isAttackReset", 1.6f);
}
}
void isAttackReset()
{
isAttackCD = false;
}
void animationReset()
{
anim.SetInteger("PholParameter", 0);
}
void Damage()
{
targetTWO.GetComponent<EnemyScript>().DecreaseHP(PlayerDamage);
}
}
The second
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Phol_MoveAnimation : MonoBehaviour {
int resolution = 15;
int CheckTargetLayer = (1<<8)|(1<<10)|(1<<11)|(1<<12)|(1<<13);
int SearchObjectLayer = (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
bool isMovingPosition , isMovingObject ;
float speed = 15 ;
float angleRange = 90, angle = 0, range = 3, height = 1; /* การตั้งค่าของ ray ของ MovePlayerObject() */
Vector3 TargetPosition;
GameObject TargetObject;
Animator anim;
public List<GameObject> seeingObj = new List<GameObject>();
void Awake()
{
anim = GetComponent<Animator>();
}
void Start ()
{
isMovingObject = false;
isMovingPosition = false;
}
void Update ()
{
seeingObj.Clear();
for (int ct1 = 0; ct1 < resolution; ct1++)
{
for (int ct2 = -1; ct2 <= 1; ct2 += 2)
{
angle = angleRange / resolution * ct1 * ct2;
Ray ray = new Ray(transform.position, new Vector3(Mathf.Sin((angle + transform.eulerAngles.y) * Mathf.Deg2Rad), height, Mathf.Cos((angle + transform.eulerAngles.y) * Mathf.Deg2Rad)));
RaycastHit hitData;
if (Physics.Raycast(ray, out hitData, range, SearchObjectLayer))
{
if (seeingObj.Contains(hitData.collider.gameObject) == false)
seeingObj.Add(hitData.collider.gameObject);
}
}
}
if (Input.GetMouseButton(0))
{
CheckTargetPosition();
}
}
void FixedUpdate()
{
if(isMovingPosition)
{
MovePlayerPosition();
}
else if(isMovingObject)
{
MovePlayerObject();
}
}
void CheckTargetPosition() /* ตรวจสอบ layer ของตำแหน่งที่กดลงไปโดยใช้ physics.raycast */
{
RaycastHit CheckTargetHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray , out CheckTargetHit , 1000 , CheckTargetLayer))
{
if(CheckTargetHit.collider.gameObject.layer == 8) /* Ground Layer */
{
if(!gameObject.GetComponent<Phol_DamageBuffDebuff>().isAttackCD)
{
isMovingObject = false;
TargetPosition = new Vector3(CheckTargetHit.point.x, CheckTargetHit.point.y, CheckTargetHit.point.z);
isMovingPosition = true;
}
}
else /* Object-Unselect Object-Select Enemy NPC Layer */
{
isMovingPosition = false;
TargetObject = CheckTargetHit.collider.gameObject;
isMovingObject = true;
}
}
}
void MovePlayerPosition()
{
if (Vector3.Distance(transform.position, TargetPosition) <= 1)
{
anim.SetInteger("PholParameter", 0);
isMovingPosition = false;
}
else if (Vector3.Distance(transform.position, TargetPosition) > 1)
{
Quaternion newRotation = Quaternion.LookRotation(TargetPosition - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
anim.SetInteger("PholParameter", 1);
transform.position = Vector3.MoveTowards(transform.position, TargetPosition, speed * Time.deltaTime);
Debug.DrawLine(transform.position, TargetPosition, Color.red);
}
}
void MovePlayerObject()
{
if(seeingObj.Contains(TargetObject))
{
anim.SetInteger("PholParameter", 0);
isMovingObject = false;
if(TargetObject.tag == "Enemy")
{
gameObject.GetComponent<Phol_DamageBuffDebuff>().Attack(TargetObject);
}
}
else
{
Quaternion newRotation = Quaternion.LookRotation(TargetObject.transform.position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
anim.SetInteger("PholParameter", 1);
transform.position = Vector3.MoveTowards(transform.position, TargetObject.transform.position, speed * Time.deltaTime);
}
}
void OnDrawGizmos()
{
for (int ct1 = 0; ct1 < resolution; ct1++)
{
for (int ct2 = -1; ct2 <= 1; ct2 += 2)
{
angle = angleRange / resolution * ct1 * ct2;
Gizmos.color = Color.yellow;
Gizmos.DrawRay(transform.position, range * new Vector3(Mathf.Sin((angle + transform.eulerAngles.y) * Mathf.Deg2Rad), height, Mathf.Cos((angle + transform.eulerAngles.y) * Mathf.Deg2Rad)));
}
}
}
}
If you want more information I will post it. Or you can just tell me which function to deal with this problem. Thanks.