Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So Im trying to write this code where the character's passive skill gets interrupted when he is hit. The way I want to do that is by having the program grab the characters health value from the health variable and check against it every frame to see if its dropped.
However, after researching I cant seem to find a way to actually do this. Grab a number from the variable without the new variable being effected by it.
Any ideas?
During each frame use another variable to store the last known value of the health variable, then compare the variables to see if they have different values. For example:
int health = 100;
int lastFrameHealth;
void Start ()
{
lastFrameHealth = health;
}
void Update ()
{
if (health < lastFrameHealth)
{
StartCoroutine("InterruptPassiveSkill", 5f);
}
lastFramehealth = health;
}
void OnCollisionEnter(Collision collision)
{
health -= 10;
}
IEnumerator InterruptPassiveSkill (float seconds)
{
// insert code to deactivate passive skill here:
yield return new WaitForSeconds(seconds); // delay for number of seconds
// insert code to reactivate passive skill here:
}
The coroutine used in this example is a nice, easy way to add a delay. Coroutines also make simple state machines much quicker to create. Coroutines are a Unity feature, non-native to C#.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to make a lifesteal system in my game,I dont know how to do it. After a lot of reasearch on google, I found nothing.Can anyone help on how to do it?
I don't mean to give me the code ofcurse but to help me just think about how it will work. I work on C#
The game is like this:
My player have some stats like health, damage, etc. The player attacks the enemy with a sword. Enemy has his own health and when it dies it gives xp to the player. I have a heal function in my player so heal can be added via Health potions. And the question is how is it supposed to heal the player based on his hits? I mean you land a hit = 20 damage you get back 2hp(e.g 10% lifesteal).
I think that you have to execute the health function everytime that the player attacks the enemy, you can do it like this:
public float playerHealth = 100;
int percentage = 20;
public void healPlayer()
{
playerHealth += (attackDamage * (percentage / 10));
}
This code is adding the 20% of the amount of damage that the player has done to the enemy, dividing the percentage between 10 and multiplying it by the amount of the damage.
You can add experience to the player when the enemy dies, executing a function like this:
public float playerExperience = 10;
public void addExperience()
{
playerExperience += 2;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to make a character move a certain distance then change direction when they travel a certain distance... the logic here in my code might be wrong I still have to work on that that's not the issue, the problem is my if statement doesn't execute
public class EnemyControl : MonoBehaviour
{
private int xMoveDirection=-1;
private float x;
void Start()
{
x=gameObject.transform.position.x;
}
void Update()
{
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(xMoveDirection,0);
if(x==0.00){
Debug.Log("helloimhere");
xMoveDirection=0;
x=x+1;
}
Any direct comparisons against floating point values will most likely fail, you're better off defining a range of values that are "close enough", for example Math.Abs(x) < 0.0001.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Recently I have been getting into Unity and have started making my first game. I am really happy with it but recently I have been getting really annoyed.
I have made a level system in my game (once you complete the first level (all level are separate scenes)you move on to the next level). I have found out how to use application.loadlevel, etc. But I also want the level menu (where you can select your past levels or current one by clicking a button) to turn intractability on once you have completed the previous level by picking up a cube. Unfortunately I don't have any idea how to do it as all my scripts have failed.
Please help me and thanks in advance and I am a beginner so don't explain things too advanced. Just tell me what to do and what I need to write in my scripts. If I have to use prefabs please tell me how as that confuses me as well.
First, you need to save somewhere that you have completed a level. This information must be saved in a persistent manner, otherwise, your player will have to restart your whole game each time he launches your game. There are many ways to do so, but PlayerPrefs could be a starting point.
Once any level is completed (before loading the next scene), call the following function :
public void OnLevelCompleted()
{
// Retrieve name of current scene / level
string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
PlayerPrefs.SetInt( sceneName, 1 ) ; // Indicates the level is completed
}
Then, in your home scene, attach a script to your buttons with the following piece of code :
public string SceneName ; // Indicate which level this button must load once you click on it. Be carefull, the name must be the same as in your Build Settings
protected void Awake()
{
UnityEngine.UI.Button button = GetComponent<UnityEngine.UI.Button>();
if( button != null )
{
// Make the button load the given scene
button.onClick.AddListener( () => UnityEngine.SceneManagement.SceneManager.LoadScene( SceneName ) ) ;
// Make the button interactable only if the given scene / level has been completed
button.interactable = PlayerPrefs.GetInt( SceneName ) > 0 ;
}
else
Debug.LogWarning("No button component attached", this ) ;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am making a robot battling game where I want the enemy to randomly move and then sometimes travel towards the enemy. Code that I want the movement to be in.
else if (avoid == false)
{
transform.LookAt(target);
transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
currentLerpTime = 0;
}
This code just makes the AI move towards the player but I also want it occasionally to move in a random direction and then change direction occasionally. How would I do this?
To make it move in a random direction, the variable you need to edit is the vector.
else if (avoid == false)
{
transform.LookAt(target);
transform.Translate(Vector3.forward*Time.deltaTime*Speed);
// ^^^^^^
currentLerpTime = 0;
}
The problem here though is while it moves it will continue to keep looking at the target (i am assuming this is getting called each frame). In terms of generating three random numbers, you can do this by yourself with just C# or go read this
https://docs.unity3d.com/ScriptReference/Random.html
That link should really help you out.
Hope this helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried this to get the score in my game and I tried this:
foreach (stella stella in stelle)
if (stella.rettangolo.Intersects(giocatore.rect))
{
score=score+10;
}
It functions but not well because if I stay a long time near the star the score continues to inrement even if it is not visible. how can i fix it?
You can add an extra flag that tells you whether or not the star has been picked up. This must be a field in the stella class. Let's call it...
public bool captata; //I'll go with some Google translated Italian.
When the level starts, initialize it with the value false:
foreach (stella stella in stelle)
{
stella.captata = false;
}
When the player intersects the star, you set the flag appropriately. At the same time, you only check for collision if the star hasn't been picked up yet.
foreach (stella stella in stelle)
if ((!stella.captata) && (stella.rettangolo.Intersects(giocatore.rect)))
{
score=score+10;
stella.captata = true;
}
Another way would be to remove the star from the stars collection completely.
for (int i = stelle.Count - 1; i >= 0; --i)
{
if (stelle[i].rettangolo.Intersects(giocatore.rect))
{
score = score + 10;
stelle.RemoveAt(i);
}
}
You need to change your game logic so that you either remove the "star" from the "stars" collection once a player comes near it, or so that you mark it as touched by the player.
Depending on what the logic of the game is, you can also only have the player get points when they start touching the star, but not while they are still touching it. you can have a bool, isTouchingStar and set it to true if you detect the player touching it.