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;
}
Related
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 1 year ago.
Improve this question
I want to make an object rotate on a particular axis at I tried using
void Rotate () {
transform.Rotate(0,0,0);
}
but it just rotate instantly , than i learned about Quaternion and everything went above my head , i didn't understand anything.
I just know that i know that there is a Lerp and Slerp.
But i don't know how to use them, I want to make the object rotate from its current rotation to a specific axis
with a Lerp.
Please Help Me!!!
Try to research before asking here. Googling same question will get you to the official docs of Unity about Quaternion here.
Following is the simple example to show how to rotate with transform locally and with world space.
void Update()
{
// Rotate the object around its local X axis at 1 degree per second
transform.Rotate(Vector3.right * Time.deltaTime);
// ...also rotate around the World's Y axis
transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
}
Following shows how to use Quaternion
// Interpolates rotation between the rotations
// of from and to.
// (Choose from and to not to be the same as
// the object you attach this script to)
Transform from;
Transform to;
float speed = 0.1f;
void Update()
{
transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
}
Also you need to use Time.deltaTime to rotate it smoothly. In your question you are just assigning the values and that's why you don't see it rotating.
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
I'am reviewing the code for the project.
What I want to do is the following picture.
I have some questions about the project.
As a beginner, I will not have enough explanation,
but I would appreciate it if you could give it a look.
Here are the questions:
How do I create an object called small_star as indefinitely, as shown in the picture?
Currently, prefab x, y coordinate values are centered on the scene.
How do I move an object with random coordinates like a photo? And when I look at the code, I use Mathf.Cos and Mathf.Sin. What effect does it have?
I think I should implement it, but it is too much for me to do coding.
I'm a beginner. I would really appreciate it if you could give me a specific explanation.
You dont provide much information, but this may work
public GameObject small_star;
public float xMinBoundary;
public float yMinBoundary;
public float xMaxBoundary;
public float yMaxBoundary;
void MoveSpaceShip(){
float randX = Random.Range (xMinBoundary, xMaxBoundary);
float randY = Random.Range (yMinBoundary, yMaxBoundary);
Vector2 target = new Vector2 (randX,randY)
//Option 1
//small_star.transform.Translate(target * Time.deltaTime);
//Option 2
small_star.transform.position(target)
}
You will need to adapt to your needs
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 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#.
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.