how do I duplicate an object and have it be in a different position?
I have been watching an endless runner tutorial on YouTube lately, but I am stumped on the part where the platforms are supposed to duplicate and move a little bit to the right so that the player can run endlessly. I however didn't do my platforms the same way as the guy in the video did his, I made mine in paint.net as one big platform that i wanted to use. He made his from 2d squares basically. I didn't want to do it exactly like he did but every time i use the instantiate code it either duplicates in the exact position. When I did try it his way it would duplicate them correctly and it would move right on the x axis as intended, however the platforms would be tilted 90 degrees when they should be flat. If anyone could help me with this problem i'm having I would gladly appreciate it.
This is how my platforms look and what it does with the script I have for it.
This is the code I wrote for the platforms.
using UnityEngine;
using System.Collections;
public class PlatformGenerator : MonoBehaviour {
public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;
private float platformWidth;
// Use this for initialization
void Start () {
platformWidth = thePlatform.GetComponent<BoxCollider2D> ().size.x;
}
// Update is called once per frame
void Update () {
if (transform.position.x < generationPoint.position.x)
{
transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
Instantiate (thePlatform, transform.position, transform.rotation);
}
}
}
Answer by bubzy · Nov 02, 2015 at 11:22 PM
i would make a new variable for transform and store the vector3 in that temporarily like
Transform tempTransform = new Transform();
tempTransform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
tempTransform.RotateAround(transform.position,new Vector3(0,0,1),90);
Instantiate (thePlatform, tempTransform.position, tempTransform.rotation);
or something similar :)
i need help on this too, I put in your code but i get a error on ''Transform'' after ''new'' on the first line, please help me
Your answer
![](https://koobas.hobune.stream/wayback/20220612043707im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to destroy only the collided instance of prefab and not the original one? 0 Answers
Instantiate duplicate of inactive child in GameObject 0 Answers
When I Instansiate a gameobject as a children the parent duplicates. Why? 1 Answer
"Infinite" terrain algorithm for 3D endless runner? 2 Answers
Spawning random tiles 1 Answer