Shoot
Sat Nov 30 2024 14:23:05 GMT+0000 (Coordinated Universal Time)
Saved by
@mehran
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shot : MonoBehaviour
{
public float LaunchForce = 5f; // قدرت شلیک اولیه
public float MinLaunchForce = 2f; // حداقل قدرت شلیک
public float MaxLaunchForce = 20f; // حداکثر قدرت شلیک
public float ScrollSensitivity = 1f; // حساسیت اسکرول موس
public GameObject Arrow; // تیر
public Transform ArrowSpawnPoint; // نقطه شروع تیر (ترانسفرم)
public GameObject SimpleArrow;
private GameObject currentArrow;
void Update()
{
// تغییر قدرت شلیک با اسکرول موس
AdjustLaunchForceWithMouseScroll();
if (Input.GetKeyDown(KeyCode.Q))
{
currentArrow = SimpleArrow;
Debug.Log("Switched to Simple Arrow");
}
// شلیک با دکمه Space
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}
void AdjustLaunchForceWithMouseScroll()
{
// خواندن مقدار اسکرول موس
float scroll = Input.GetAxis("Mouse ScrollWheel");
// تغییر قدرت شلیک بر اساس اسکرول
LaunchForce += scroll * ScrollSensitivity;
// محدود کردن قدرت شلیک بین حداقل و حداکثر
LaunchForce = Mathf.Clamp(LaunchForce, MinLaunchForce, MaxLaunchForce);
// نمایش مقدار قدرت شلیک در کنسول (اختیاری)
// Debug.Log("Current Launch Force: " + LaunchForce);
}
void Shoot()
{
// ایجاد یک نمونه از تیر
GameObject ArrowIns = Instantiate(Arrow, ArrowSpawnPoint.position, ArrowSpawnPoint.rotation);
Rigidbody2D rb = ArrowIns.GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.isKinematic = false;
rb.AddForce(ArrowSpawnPoint.right * LaunchForce, ForceMode2D.Impulse);
}
}
}
content_copyCOPY
Comments