How to move an object to random coordinates in Unity? [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 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

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

if statement not executing in c# 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 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.

How to get a down vector just from a normal vector? [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 3 years ago.
Improve this question
I have got a 3D game running. The red rectangle is my player on some kind of ramp or house roof. He is not rotated. Now when he falls on such a ramp I can get the green vector by the RaycastHit normal.
But what I want to get is the purple vector, so I can give my player the velocity to slide down. How do I get that vector or is it impossible with the variables given?
The purple vector is the closest vector to Vector3.down that is tangent to the roof.
Edit: I guess I figured out how to get to that Vector but I still don't understand how to calculate it. In the second picture vector a and b is given. Now I need to get vector c, which is 90 degrees from a to b.
Figured it out. If anyone is wondering: I just used the Vector3.RotateTowards like this:
rigid.velocity = Vector3.RotateTowards(hit.normal, -transform.up, Mathf.PI / 2, 0);
The vector you are looking for is the vector orthogonal to the normal of the surface that is closest to the direction of gravity (typically Vector3.down). You can use vector cross product to find this:
Vector3 surfaceNormal;
Vector3 directionOfGravity = Vector3.down;
Vector3 slopeSideways = Vector3.Cross(directionOfGravity, surfaceNormal);
if (slopeSideways != Vector3.zero)
{
Vector3 slopeDown = Vector3.Cross(surfaceNormal, slopeSideways).normalized;
}
else
{
// surface is normal to the direction of gravity, there is no downwards slope.
}

Unity; Random Object Movement [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 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.

Categories

Resources