Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by dimasapry · Oct 26, 2018 at 11:48 AM · unity 2dendless runnerenemy spawn

How to Make Game Object (Enemy) Move (Right to Left) in 2D Endless Runner Game?

Hello right now i'm making 2d endless runner game when the player move to the right automatically, so i do have code that will does object pooling and generate at the generation point of the enemy, but it won't move to the left. The object doesn't have RigidBody2D, and i don't have idea to implement this code to mine correctly.

EnemyPool.GetPooledObject= transform.Translate(Vector3.left * Time.deltaTime, Camera.main.transform);

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KayuPlatformGenerator : MonoBehaviour {
 
     public GameObject thePlatform;
     public Transform generationPoint;
     private float distanceBetween;
     private float platformWidth;
     public float distanceBetweenMin;
     public float distanceBetweenMax;
     public float randomEnemyThreshold;
     public ObjectPooler enemyPool;
     public ObjectPooler theObjectPool;
 
     void Start () {
         platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
     }
 
     void Update () {
         if (transform.position.x < generationPoint.position.x) {
             distanceBetween = Random.Range (distanceBetweenMin, distanceBetweenMax);
             transform.position = new Vector3 (transform.position.x + (platformWidth / 2) + distanceBetween, transform.position.y, transform.position.z);
             GameObject newPlatform = theObjectPool.GetPooledObject ();
             newPlatform.transform.position = transform.position;
             newPlatform.transform.rotation = transform.rotation;
             newPlatform.SetActive (true);
 
             if (Random.Range (0f, 100f) < randomEnemyThreshold) 
             {
                 GameObject newEnemy = enemyPool.GetPooledObject ();
                 float enemyXPosition = Random.Range (-platformWidth / 2+8f, platformWidth / 2-8f);
                 Vector3 enemyPosition = new Vector3 (mbakEnemyXPosition, Random.Range(9f, 11f), 0f);
                 newMbakEnemy.transform.position = transform.position + mbakEnemyPosition;
                 newEnemy.transform.rotation = transform.rotation;
                 newEnemy.SetActive (true);
             }
             transform.position = new Vector3 (transform.position.x + (platformWidth / 2), transform.position.y, transform.position.z);
         }
     }
 }

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 tormentoarmagedoom · Oct 26, 2018 at 01:45 PM 0
Share

good day.

If your spawning object is always at the same distance of the player (for example 150 units in front of player), you can just make something like this in the Update:

 Vector3 SpwnObjePos = new Vector3 (Player.transform.position.x, Player.transfomr.position.y+150, Player.transform.position.z);
 spawningObject.transform.position = SpwnObjePos ;
avatar image dimasapry tormentoarmagedoom · Oct 27, 2018 at 03:10 PM 0
Share

Thank you for your answer? i wanna ask why you add 150 value at y position? it makes the enemy spawn go up outside the screen, and i try to add it at x position ins$$anonymous$$d, yes it will respawn, but it doesnt have it own speed , and it looks like just a tree that passing by ins$$anonymous$$d of living enemy that is passing. @tormentoarmagedoom

0 Replies

· Add your reply
  • Sort: 

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

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get a player to move in one direction and around it in the same time unity 2D 1 Answer

Making an enemy go back to its spawn after losing target 0 Answers

Pause scene when character hit object, then resume it from another scene 0 Answers

stopping 1 animation and playing another 0 Answers

increasing speed of a player based on score? 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