36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ItemAddForceTrigger : MonoBehaviour
|
|
{
|
|
// The force magnitude to apply to the item in the Z-axis
|
|
public float forceMagnitude = 10f;
|
|
|
|
// Reference to the Rigidbody of the item to be affected
|
|
public Rigidbody itemRigidbody;
|
|
|
|
public bool DoOnce = true;
|
|
private bool DidOnce;
|
|
|
|
// Called when the player enters the trigger
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (!DidOnce)
|
|
{
|
|
// Check if the player has entered the trigger (you can adjust the player's tag accordingly)
|
|
if (other.CompareTag("Player"))
|
|
{
|
|
// Add force to the item in the Z-axis
|
|
itemRigidbody.isKinematic = false;
|
|
Vector3 forceToAdd = new Vector3(0f, 0f, forceMagnitude);
|
|
itemRigidbody.AddRelativeForce(forceToAdd, ForceMode.Impulse);
|
|
if (DoOnce)
|
|
{
|
|
DidOnce = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|