Hello everyone,
I’m developing a game on Unity and I need help implementing a force potion script. Here’s what I’m trying to do:
- Strength Potion: A potion that, when touched by the player, adds a random amount of strength to their character.
- Variability: The strength gained should be random, but within specific limits (for example, between 0 and 100).
- Respawn: After being hit, the potion should disappear for a short time before reappearing at a new location.
- Console Log: Optionally, display in the Unity console the strength gained each time a player touches the potion.
I already wrote a script, but when the player steps on the potion, nothing happens. Here is the current script:
using UnityEngine;
public class PotionDeForce: MonoBehaviour
{
public int forceGagneeMin = 0;
public int forceGainedMax = 100;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Generate a random number between forceGagneeMin and forceGagneeMax inclusive.
int forceGagnee = Random.Range(forceGagneeMin, forceGagneeMax + 1);
// Add the gained strength to the player (you need to implement player management).
// For example, if the player has a "Player" class with an "AddForce" method, you could do something like:
// other.GetComponent<Player>().AddStrength(strengthGained);
// Reappearance of the potion after a certain delay.
Invoke("ReappearancePotion", 2f);
// Optionally, you can display the strength gained in the console.
Debug.Log("Strength gained: " + forceGained);
}
}
void ReappearancePotion()
{
// Code to move the potion to a new position or reset its original position.
// You should implement this according to your game design.
}
}
I welcome any advice or corrections that could improve this script. Thank you in advance for your help !
Adapt it as needed and be sure to include specific details about your current implementation for more accurate answers. But be careful the script will be put in an object!
Sorry the script is in french and english pls make the script in english or french
I’am french