I'm trying to do something like this in Unity - https://youtu.be/uLZvNNW_Xoc , where when I go through the checkpoint, I get the exact mathematical result (clones of the character) of the same character who passed it.
I tried to clone them manually so that I knew how the animation and the walk would be, but when I turned on the game, it all shattered, the characters started to run around and bounced off each other.
You can Clone GameObjects with GameObject.Instantiate()
More details here: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
You can try setting a specific position for each cloned GameObject, it sounds like u are cloning them too close to each other.
Here's a little example of what I mean:
GameObject characterToClone = GameObject.Find("NameOfYourCharacterGameObject");
for(int i = 0; i < amountOfClonesNeeded; i++)
{
Vector3 newPosition = characterToClone.transform.position + new Vector3(10, 0, 0);
characterToClone = GameObject.Instantiate(characterToClone, newPosition, Quaternion.identity);
}
Like this you can Clone your Character multiple times where every new Clone should be 10 units appart from the previos one! You probably have to adjust the offset yourself though to make it fit perfectly for your game.
I hope I was able to help you!
Related
This question already has answers here:
Physics2D.OverlapCircleAll not detecting other gameobjects
(4 answers)
Closed 1 year ago.
I want to spawn some balls (with CircleCollider2d and static Rigidbody2D) with this code. I use a button to see if it works and when I use it everything is ok. But when I use a loop to create a bunch of balls in a single frame, they appear overlapping. I think it is because you have to wait for the physics to be updated, but I need to spawn all balls at the same time. Any ideas or solutions?
It is my first question and I do not speak English well, but I hope I have explained it well. Thanks a lot
public void CreatePointBall()
{
Vector2 pos;
float x, y;
int tries = 0;
do
{
tries++;
x = Random.Range(MIN_POS_X, MAX_POS_X);
y = Random.Range(MIN_POS_Y, MAX_POS_Y);
pos = new Vector2(x, y);
} while (Physics2D.OverlapCircle(pos, radius, LayerMask.GetMask("PointBall")) != null && tries < MAX_TRIES);
if(tries == MAX_TRIES) Debug.Log("MAX TRIES WITHOUT A CORRECT POS");
GameObject go = Instantiate(pointBallPrefab);
go.name = "PointBall";
go.transform.parent = transform;
go.transform.position = pos;
pointBallsList.Add(go);
}
You can try to use Physics.SyncTransforms. Or Physics.Simulate
Actually, it's not a good practice to do this, it can hurt the performance a lot.
If you not need to spawn spheres immediately, in a single frame, you can put your code into coroutine, and after spawning each ball, wait for physics update: yield return new WaitForFixedUpdate();
If all of your spheres has a same scale, you can easily use a math instead of physics. For this, you need just to check the distance from current point to all other points.
It can be expensive, if you have a really large amount of points - so you can optimize it, keeping your points sorted - if your points array is sorted, you only need to compare 2 distances, next larger and previous smaller. But it can be tricky to implement.
Also, if you'll compare many distances, do not use Vector2.magnitude - use Vector2.sqrMagnitude, it's much faster.
I'm making a very simple platformer game, not to publish or anything like that but rather to experiment with Unity and C#, and I've been trying to make a dash mechanic. two ways that I tried to go about this were
Getting the players position and teleporting them in any one direction, depending on the direction of the dash, didn't work because I couldn't figure out how to find the player's position
Making the player move fast in any one direction, didn't work because of how the rest of the movement script works.
I would prefer to use the first option, does anyone know how to find the players location? I think I was able to find the transform position, but I didn't know how to use it since it was 3 values, x, y, and z, rather than one, and I didn't know how to only get 1. Thanks in advance!
Not a definitive answer, since this depends on the code you are using and i have not shown how to dash, there is a lot of camera code and i am not coding unity anymore, so guessing this out without tests seem wrong, i would recommend adding the code, but the first option is simple enough to an answer.
In the player script, use transform.position, this will not fail since all Unity GameObjects have a world position, and therefore a transform.
// not sure if i spelled correctly
public class Player: Monobehaviour {
/* ... */
void Dash () {
// transform.position is the current position as a 3D vector
var pos = transform.position;
// to access its x, y and z do this:
var x = pos.x;
var y = pos.y;
var z = pos.z;
}
}
So I am a beginner to unity and wanted to learn smth new, so I decided to make a clone of this game
https://www.pinterest.ru/pin/499618152412280381/
Almost everything is done, but unfortunately, I can't find out how do I spawn these squares with.. uh.. random rotation angle? Bruh I can't explain it better. So this is the script that I have:
private void SpawnDefaultCube()
{
GameObject a = Instantiate(defaultCube) as GameObject;
a.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * 2, Camera.main.transform.position.z+1);
}
private void SpawnScoreCube()
{
GameObject b = Instantiate(scoreCube) as GameObject;
b.transform.position = new Vector3(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * 2, Camera.main.transform.position.z+1);
}
Now the cubes are falling down, but I want them to fall like in the video. What can I make to do so? Is there some kind of function? Thanks in advance, I've been struggling with this for several days now.
In the update of your cube, you could use Transform.Rotate.
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
For the random element of it, get a random number between -1 and 1 and multiply the rotation speed you decide with it.
I trust that there are amazing people here that can solve this problem
I have a List of GameObjects that have been made in my first script;
public List<GameObject> _recordinglist = new List<GameObject>();
Then a button creates a clone and adds to the _recordinglist
recordedObject = Instantiate(mynewPrefab, new Vector3(0, 0, 0), Quaternion.identity);
// adding to created list
_recordinglist.Add(recordedObject);
Now in the second Script
I have a working script that
changes the GameObject into other GameObjects. E.g. Turning a Kick into a Clap with the click of different buttons where I would change the Game Object in the Inspector
However
Trying to do
GameObject = List[0];
Gives Errors even though it is initialised
Also I'm not sure how to click/cylce through the list with a button?
Some clear guidance for a relatively beginner coder would be greatly appreciated.
I have already asked in the Unity Forum, but it lead to confusion. I have also emailed directly and the answers aren't detailed enough. I have also checked around and haven't seen exactly what I need.
In other words,
I'm Trying to make
Recording1 = Recording2 or 3, 4 , 5, 6, then start at 1 as I select through.
Instead of just Recording = Recording 2
Not sure I understood, but I think this is what you want:
GameObject r = _recordings[0]
(In the question you forgot to give the variable a name)
to iterate through the list:
int i = 0;
GameObject go = _recordings[i];
i = (i + 1) % _recordings.Length;
The modulus (%) symbol returns the division remainder, so it makes sure i would just be set back to 0 when it gets to _recordings.Length
My question is about if there is a way to know the coordinates of the corners of a gameobject. What I have is three Vuforia AR targets and a gameobject, a cube in this case.
What I need to achieve, is so that when I move the targets around, the corners of the cube would follow the targets, eg. the cube would be as wide as the space between the targets.
Right now how it does it, is that it checks the distance between the targets and sets the scale of the cube according to it. This results in the cube being expanded always from its set position, which makes the positioning of the targets awkward in real life.
Here's a couple of pictures showing what it does now, taken during execution:
Here is the code attached to the cube-object:
using UnityEngine;
using System.Collections;
public class show : MonoBehaviour {
float distancex;
float distancez;
// Use this for initialization
void Start () {
renderer.enabled = false;
}
// Update is called once per frame
void Update () {
if (GameObject.Find ("target1").renderer.enabled && GameObject.Find ("target2").renderer.enabled &&
GameObject.Find ("target3").renderer.enabled && GameObject.Find ("target4").renderer.enabled)
{
renderer.enabled = true;
}
distancex = Vector3.Distance ((GameObject.Find ("target1").transform.position), (GameObject.Find ("target2").transform.position));
distancez = Vector3.Distance ((GameObject.Find ("target2").transform.position), (GameObject.Find ("target3").transform.position));
Debug.Log (distancex);
Debug.Log (distancez);
transform.localScale = new Vector3((distancex), transform.localScale.y, transform.localScale.z);
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, (distancez));
Debug.Log (transform.localScale);
}
}
How do I make the corners of the object follow the targets? I need the width of the side to be the width of the distance between the targets, is there any way to achieve this without using the scale?
I know this is quite some time after you asked the question, but I came across this as I was looking to sort something similar out myself.
What I found I needed (and others may be helped) is to use Renderer.bounds
What it looks like in practice for me:
void Update () {
rend = currentGameObject.GetComponent<Renderer>();
Debug.Log(rend.bounds.max);
Debug.Log(rend.bounds.min);
}
My object in this case was a quad at position 0,0,200 and a scale of 200,200,1. The output of rend.bounds.max is (100.0,100.0,200.0) the min was (-100.0,-100.0,200.0). This gives you the corner position for each corner (granted my example was in 2D space, but this should work for 3d as well).
To get it a little more specific for your example if you wanted the top right corner, you could get the XY of renderer.bounds.max, for the top left you would get the renderer.bounds.max.y and the renderer.bounds.min.x. Hope that helps!
Cheers!
Create 8 empty game objects.
Make them children of the "cube" object to be tracked.
Move them in Editor to be at the 8 corners of your tracked game object. Since they are children of the tracked object, their positions will be {+/- 0.5, +/- 0.5, +/- 0.5}.
Name them memorably (i.e., "top-left-back corner").
These 8 objects will move and scale with the tracked object, staying at its corners. Any time you want to know where a corner is, simply reference one or more of the 8 game objects by name and get the Vector3 for each of their transform.position.
Note: If you're doing step 5 repeatedly, you may want to avoid potentially costly GameObject.Find() calls (or similar) each time. Do them once and save the 8 game objects in a variable.
I worked my way around the problem by determining the position between the targets and counting their mean.
This is not flawless, however, since it is difficult to determine the scale factor with which the models would accurately follow the targets' corners, when bringing external models to use with the system.