// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) //{ // //} // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) //{ // //} // OnStateExit is called when a transition ends and the state machine finishes evaluating this state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)//Через фниматор получаем доступ к Attack_Controller { Attack_Controller attack_Controller = animator.transform.root.GetComponent <Attack_Controller>(); attack_Controller.FinishAttak(); }
private Attack_Controller _attackController; //Добавляем объект со скрипта Attack_Controller private void Start() { _attackController = transform.root.GetComponent <Attack_Controller>();/*Один из способов передачи ссылки на компонент, при использовании root он пробегается по родительским * объектам отдавая корневой объект на сцене находящийся выше всех в иерархии */ }
//shoot player projectile private void PlayerRangedAttack() { if (rangedCoolDownInSeconds == 0) { if (flipSpriteOnVelocity != null) { flipSpriteOnVelocity.forceLookRight = !spriteRenderer.flipX; } ////original attack pos code //Vector3 mouseScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); //Vector2 mouseposition = Camera.main.ScreenToWorldPoint(mouseScreenPosition); //mouseposition = (mouseposition - (Vector2)transform.position).normalized * offset; //GameObject childInstance = Instantiate(rangedAttack.gameObject, mouseposition + (Vector2)transform.position, transform.rotation); ////discussed pos attack code //GameObject childInstance = null; //if (spriteRenderer.flipX != true) // childInstance = Instantiate(rangedAttack.gameObject, Vector2.right * offset + (Vector2)transform.position, transform.rotation); //else // childInstance = Instantiate(rangedAttack.gameObject, Vector2.left * offset + (Vector2)transform.position, transform.rotation); //playerMovement.MovementState = CustomGCOTypes.MovementState.Disabled; ////proposed attack pos code ver 1 //GameObject childInstance = null; //Vector3 mouseScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); //Vector2 mouseposition = Camera.main.ScreenToWorldPoint(mouseScreenPosition); //Vector2 normalPos = (mouseposition - (Vector2)transform.position).normalized; //if (spriteRenderer.flipX != true && normalPos.x < 0) // normalPos.x = normalPos.x * -1; //else if (spriteRenderer.flipX == true && normalPos.x > 0) // normalPos.x = normalPos.x * -1; //mouseposition = normalPos * offset; //childInstance = Instantiate(rangedAttack.gameObject, mouseposition + (Vector2)transform.position, transform.rotation); ////proposed attack pos code ver 2 //GameObject childInstance = null; //Vector3 mouseScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); //Vector2 mouseposition = Camera.main.ScreenToWorldPoint(mouseScreenPosition); //Vector2 normalPos = (mouseposition - (Vector2)transform.position).normalized; //if (spriteRenderer != null && flipSpriteOnVelocity != null) //{ // if (normalPos.x < 0) // { // spriteRenderer.flipX = true; // flipSpriteOnVelocity.forceLookRight = false; // } // else // { // spriteRenderer.flipX = false; // flipSpriteOnVelocity.forceLookRight = true; // } //} //mouseposition = normalPos * offset; //childInstance = Instantiate(rangedAttack.gameObject, mouseposition + (Vector2)transform.position, transform.rotation); //proposed attack pos code ver 3 GameObject childInstance = null; Vector3 mouseScreenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); Vector2 mouseposition = Camera.main.ScreenToWorldPoint(mouseScreenPosition); Vector2 normalPos = (mouseposition - (Vector2)transform.position).normalized; if (spriteRenderer != null && flipSpriteOnVelocity != null) { if (normalPos.x < 0) { spriteRenderer.flipX = true; flipSpriteOnVelocity.forceLookRight = false; } else { spriteRenderer.flipX = false; flipSpriteOnVelocity.forceLookRight = true; } } if (spriteRenderer.flipX != true) { childInstance = Instantiate(rangedAttack.gameObject, Vector2.right * offset + (Vector2)transform.position, transform.rotation); } else { childInstance = Instantiate(rangedAttack.gameObject, Vector2.left * offset + (Vector2)transform.position, transform.rotation); } Vector3 temp = childInstance.transform.position; temp.z = transform.position.z; childInstance.transform.position = temp; //childInstance.GetComponent<Rigidbody2D>().velocity = rangedAttack.speed * mouseposition.normalized; childInstance.transform.parent = transform; rangedCoolDownInSeconds = rangedCoolDownInSecondsDefault; SpriteRenderer childSprite = childInstance.GetComponent <SpriteRenderer>(); if (childSprite != null) { childSprite.flipX = spriteRenderer.flipX; } Attack_Controller childAttack_Controller = childInstance.GetComponent <Attack_Controller>(); if (childAttack_Controller != null) { childAttack_Controller.whenDestroyed = ResetForceLook; } //Debug.Log(spriteRenderer.flipX); } }