Unity; Random Object Movement [closed] - c#

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.

Related

I'm writing an algorithm to determine if someone has been hit by a bullet or not [closed]

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 last month.
Improve this question
Bullet
I've been thinking about it for a while,
but I can't find an answer, so I'm asking a question.
I'm making a game with C# opengl(opentk).
At first, I tried to search the coordinates of the bullet for each pixel to see
if it hit the enemy.
That method required too extensive a search.
I'm not asking you to code.
Just need some tips.
Any help would be appreciated.
Rotate the scene so that the trajectory becomes vertical. This is done by applying the transformation
X' = ( u.X + v.Y) / √(u²+v²)
Y' = (- v.X + u.Y) / √(u²+v²)
to all points. ((u, v) defines the shooting direction.)
Now it suffices to check if
Xc' - R < Xo' < Xc' + R
and
Yo' < Yc'

How do I make an object move to a certain position after being hit by the player? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to make a maze and I want an invisible block to move to a certain position and to turn visible after the player collides with it, however even though I thought I had the right coordinates in my code the object keeps moving to (30,90,8.75) and those aren't the right coordinates I input. Please help. I am a beginner. Many thanks :)
MeshRenderer visibility;
void Start()
{
visibility = GetComponent<MeshRenderer>();
visibility.enabled = false;
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player" && gameObject.tag == "Invisible")
{
movePlayer();
visibility.enabled = true;
}
}
void movePlayer()
{
transform.position = new Vector3(19.5f, 45.478f, 11f);
}
Image
Due to the object being a child of another GameObject you have to use localPosition and not position. See: Transform.localPosition

How can I make a Lifesteal system inside unity using C#? [closed]

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;
}

How to move an object to random coordinates in Unity? [closed]

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

How can i get the right score? [closed]

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.

Categories

Resources