ArrowScript hit
Sat Nov 30 2024 14:24:13 GMT+0000 (Coordinated Universal Time)
Saved by
@mehran
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrowScript : MonoBehaviour
{
private Rigidbody2D rb;
private bool hasHit = false;
private float horizontalInput;
void Start()
{
// گرفتن ریگیدبادی
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// اگر تیر هنوز به چیزی برخورد نکرده، ردیابی زاویه حرکت را انجام بده
if (!hasHit)
{
AlignWithMovement();
}
}
void AlignWithMovement()
{
// بررسی جهت حرکت و چرخاندن تیر
if (rb.velocity.magnitude > 0.1f)
{
float angle = Mathf.Atan2(rb.velocity.y, rb.velocity.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
private void OnCollisionEnter2D(Collision2D col)
{
// توقف تیر پس از برخورد
hasHit = true;
rb.velocity = Vector2.zero; // توقف حرکت
rb.isKinematic = true; // غیر فعال کردن فیزیک
}
}
content_copyCOPY
Comments