Currently I spawn enemies every 2 seconds and randomize their Y position upon spawn and then the object just goes right.
I wanted to spice up things a little and make the object move up or down randomly, but still going right.
I saw some solutions using physics, but tbh I want to avoid physics in this little demo im trying.
I toyed with transforms and translates, however it just teleports the objects.
Here is how they behave now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
[SerializeField]
private float speed = 2.0f;
void Start() {
GameController game = GameObject.Find("GameController").GetComponent<GameController>();
float randomY = Random.Range(1, game.topBoundary);
transform.position = new Vector3(game.leftBoundary, randomY, 0);
//InvokeRepeating("RandomMovement", 1.0f, 1.0f);
}
void Update() {
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.tag == "PlayerBullet") {
Destroy(this.gameObject);
}
}
//void RandomMovement() {
// float randomY = Random.Range(0, game.topBoundary);
// transform.position = new Vector3(game.rightBoundary, randomY, 0);
//}
}
I commented one of the failed, desperate attempts to make it work.
I’m going to assume you’re using a Rigidbody2D and that your script is attached to the GameObject.
private Rigidbody2D playerRb;
private float playerSpd;
private int randomDirNum;
private int randomDirNum2;
private void Start()
{
playerRb = GetComponent<Rigidbody2D>();
playerSpd = 10f;
randomDirNum1 = Random.Range(-1,2);
if(randomDirNum1 == 0)
{
while(randomDirNum1 == 0)
{
randomDirNum1 = Random.Range(-1,2);
}
}
randomDirNum2 = Random.Range(-1,2);
if(randomDirNum2 == 0)
{
while(randomDirNum2 == 0)
{
randomDirNum2 = Random.Range(-1,2);
}
}
}
private void Update()
{
// playerRb.velocity = Vector2.right * playerSpd;
playerRb.velocity = Vector2(randomDirNum1 * playerSpd,randomDirNum2 * playerSpd);
}
So here I commented out the regular move to the right code. If you always want it to move to the right, then you would uncomment that and remove randomDirNum1 and replace it with a positive “1”. Your random number will determine the Y-axis direction. I believe this will be more of slanted movement. (You’d move both right and either up/down at the same time, like a slope)
You can easily manipulate this code to refresh the randomDirNum2 and constantly change directions.
NOTE-
I set it up to where you’ll always get either -1 or 1 for both variables because 0 means no movement.
Vector2.right = Vector2(1,0)
Vector2.left = Vector2(-1,0)
Vector2.up = Vector2(0,1)
Vector2.down = Vector2(0,-1)
Thank you for suggestion Tim, but I wanted to avoid using any RigidBodies etc.
I finally found something that works, don’t know about performance or code quality, but for now it works, I will probably revisit it in the future or if someone suggests some improvments
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
[SerializeField]
private float speed = 2.0f;
[SerializeField]
public GameObject bulletPrefab;
private float y;
void Start() {
GameController game = GameObject.Find("GameController").GetComponent<GameController>();
float randomY = Random.Range(1, game.topBoundary);
transform.position = new Vector3(game.leftBoundary, randomY, 0);
InvokeRepeating("RandomMovement", 1.0f, 1.0f);
InvokeRepeating("Shooting", 2.0f, 2.0f);
}
void Update() {
transform.Translate(new Vector3(1, y, 0) * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.tag == "PlayerBullet") {
Destroy(this.gameObject);
}
}
void Shooting() {
Instantiate(bulletPrefab, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);
}
void RandomMovement() {
y = Random.Range(-1.0f, 1.0f);
}
}
I see what you are doing. You’d rather design your own physics rather than use the built in physics of a Rigidbody component. However, I think you will need to change your random range to (-1.0f, 2.0f) because otherwise you’re always going to get either -1f or 0f in that bracket. (If I remember correctly). I think you need to at least go 1.1f. Using floats here, I also believe you’ll return 0.5f or anything inbetween -1 and 1. I think MAYBE it may be better to use integers here. Just a thought, amigo 