Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raddish · Jul 22, 2014 at 03:39 AM · 3dspawningplatform

Spawn another platform infront of the original platform

Hi everyone, I'm new to C# unity programming and I'm learning to create a 3D endless straight running platform track but instead of the player moving, I'll want to make the platform move instead. However the only problem I'm facing is spawning another platform infront of the original platform when the original platform moves out of the camera view or a certain axis.

This is my current code to make the platform move and I would like to spawn another platform infront of the original platform once it goes out of camera view.

Forgive my simple coding as I'm relatively new to programming.

using UnityEngine;

using System.Collections;

public class Platform : MonoBehaviour {

 public float speed = 6f;


 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     transform.Translate (Vector3.back * Time.deltaTime * speed);
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image calebheale · Jul 22, 2014 at 04:08 AM 0
Share

Are all the platforms going to be the same? If so I would recommend just having two platforms and moving the last in front of the first after it goes out of view. This would cut down on object creation. There is actually a pretty good example of this in the scrolling backgrounds live training.

avatar image Raddish · Jul 22, 2014 at 01:47 PM 0
Share

Thanks a lot for the tutorial and yes, they are going to be the same however I can't seem to be able to move the first platform which went out of the camera screen first to be moved back in front of the second platform which is about to go off screen.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Franky_Cranky_Man · Jul 22, 2014 at 04:00 AM

Hi Raddish,

Your code right now takes a platform and moves it. It does not spawn any new objects. Here is how you could go about it. Apologize for any typos as I am not using an IDE to make this code.

 public float speed;
 
 // Prefab of the platform you want to create
 public GameObject platformPrefab;
 
 // Reference to the last platform you created
 private GameObject lastCreatedPlatform;
 // Stored position of the last platform
 private Vector3 lastPlatformPosition;
 
 //Position offset of each platform
 public Vector3 offset;
 
 void Awake()
 {
   speed = 6f;
   lastPlatformPosition = new Vector3(0,0,0);
   offset = new Vector3(1,0,0);
 {
 
 void Start()
 {
   // Runs CreatePlatform() every second
   InvokeRepeating("CreatePlatform", 0, 1f);
 }
 
 CreatePlatform()
 {
   lastCreatedPlatform = (GameObject)Instantiate(platformPrefab, lastPlatformPosition + offset, Quaternion.Identity);
 
   lastPlatformPosition = lastCreatedPlatform.transform.position;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raddish · Jul 22, 2014 at 03:00 PM 0
Share

Thanks a lot for your time and help Franky_Cranky_$$anonymous$$an but I can't seem to be able to spawn the platform in the Z axis as it is spawning on the X axis ins$$anonymous$$d and is it possible for me to reduce the gap in between the platforms? As the platform tend to be spawn quite a distance from the original platform. Either way, really appreciate your help a lot. Thank you for your time once again was really able to understand what you had written easily. Hope I can get better

avatar image Franky_Cranky_Man · Jul 23, 2014 at 02:33 AM 0
Share

It's spawning in the x axis because of this line:

 offset = new Vector3(1,0,0);

Just modify this vector to whatever direction(s) you want it to move in like offset = new Vector3(0,0,1);

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with the randomly spawning enemies 0 Answers

3D Platform Tutorial Problem 1 Answer

How to make a 3D character move only left and right in 3D environment? 1 Answer

Player movement on tilting platform 0 Answers

3d Platform Tutorial is broken right at the start 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges