반응형
- 코드 작성
FruitController.cs
using UnityEngine;
public class FruitController : MonoBehaviour
{
public float upwardForce = 5f;
void Start()
{
LaunchFruit();
}
void Update()
{
if (transform.position.y < -5f)
{
Destroy(gameObject);
}
}
void LaunchFruit()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.AddForce(Vector2.up * upwardForce, ForceMode2D.Impulse);
rb.AddTorque(Random.Range(-5f, 5f), ForceMode2D.Impulse);
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Blade"))
{
Destroy(gameObject);
// Add scoring or other effects here
}
}
}
BladeController.cs
using UnityEngine;
public class BladeController : MonoBehaviour
{
public GameObject bladeTrailPrefab;
public float minCuttingVelocity = 0.001f;
private bool isCutting = false;
private GameObject currentBladeTrail;
void Update()
{
UpdateCutting();
}
void UpdateCutting()
{
if (Input.GetMouseButtonDown(0))
{
StartCutting();
}
else if (Input.GetMouseButtonUp(0))
{
StopCutting();
}
if (isCutting)
{
UpdateBladeTrail();
}
}
void StartCutting()
{
isCutting = true;
currentBladeTrail = Instantiate(bladeTrailPrefab, transform.position, Quaternion.identity);
}
void StopCutting()
{
isCutting = false;
Destroy(currentBladeTrail);
}
void UpdateBladeTrail()
{
Vector2 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = newPosition;
float velocity = (newPosition - (Vector2)transform.position).magnitude / Time.deltaTime;
if (velocity > minCuttingVelocity)
{
currentBladeTrail.transform.position = newPosition;
}
else
{
Destroy(currentBladeTrail);
}
}
}
반응형