bow
Sat Nov 30 2024 14:22:39 GMT+0000 (Coordinated Universal Time)
Saved by @mehran
using UnityEngine;
public class Bow : MonoBehaviour
{
private Vector2 direction; // جهت چرخش
private Animator anim; // مرجع انیماتور
private bool isAttacking = false; // وضعیت حمله
private Rigidbody2D rb; // مرجع Rigidbody2D
public float horizontalInput;
public float moveSpeed = 5f; // سرعت حرکت
void Start()
{
// گرفتن انیماتور و Rigidbody2D از آبجکت
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// حرکت به چپ و راست
float horizontalInput = Input.GetAxis("Horizontal");
// اعمال سرعت به Rigidbody2D
rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);
// چرخاندن شخصیت بر اساس جهت حرکت
if (horizontalInput > 0.01f)
{
transform.localScale = new Vector2(Mathf.Abs(transform.localScale.x), transform.localScale.y);
}
else if (horizontalInput < -0.01f)
{
transform.localScale = new Vector2(-Mathf.Abs(transform.localScale.x), transform.localScale.y);
}
// فعال کردن انیمیشن دویدن
anim.SetBool("run", horizontalInput != 0);
//// وقتی دکمه ماوس فشرده میشود
//if (Input.GetMouseButton(0)) // کلیک چپ نگه داشته شود
//{
// StartAttack();
//}
//else // وقتی کلیک رها شود
//{
// StopAttack();
//}
//// در صورتی که حمله فعال نباشد، چرخش به سمت ماوس انجام شود
if (!isAttacking)
{
Aim();
}
}
void Aim()
{
// محاسبه موقعیت ماوس و کمان
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 bowPos = transform.position;
// محاسبه جهت
direction = mousePos - bowPos;
// چرخش کمان به سمت ماوس
transform.right = direction;
}
//void StartAttack()
//{
// if (!isAttacking)
// {
// isAttacking = true;
// anim.SetBool("click", true); // انیمیشن Attack
// }
//}
//void StopAttack()
//{
// if (isAttacking)
// {
// isAttacking = false;
// anim.SetBool("click", false); // بازگشت به انیمیشن Idle
// }
//}
}



Comments