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);
}
}
}
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 ;
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
Your answer
Follow this Question
Related Questions
stopping 1 animation and playing another 0 Answers
increasing speed of a player based on score? 0 Answers
Error CS0246: The type or namespace name 'ChallengeController' could not be found. 0 Answers
Player cannot double jump 1 Answer
How can I get a player to move in one direction and around it in the same time unity 2D 1 Answer