Hello Everyone
I’m Fairly new to unity so I apologize if my question is silly.
I wanted to create a 2D platformer enemy that moves between Positions A, B, C … etc. I used a simple method to do so which is completely fine and working but it has one flaw though… Its Static and each position the enemy will move to is hard coded which is fine for a simple enemy but in the game I’m building I will have many different types of enemies. This is the code I wrote:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] private Transform startPos;
[SerializeField] private Transform pos1, pos2: // Adding more positions as needed.
[SerializeField] private float speed;
[SerializeField] private float startWaitTime;
[SerializeField] private float waitTime;
private bool facingRight = true;
private Vector3 nextPos;
void Awake()
{
nextPos = startPos.position;
}
void Update()
{
if (transform.position == pos1.position)
{
if (waitTime <= 0) { nextPos = pos2.position; waitTime = startWaitTime; Flip(); }
else { waitTime -= Time.deltaTime; }
}
else if (transform.position == pos2.position)
{
if (waitTime <= 0) { nextPos = pos1.position; waitTime = startWaitTime; Flip(); }
else { waitTime -= Time.deltaTime; }
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
As I mentioned earlier the code is working fine and tested it on multiple positions, However… I want my enemy to pick a position randomly and move towards it I don’t want it to be dull as hell so I attempted this code:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] private Transform startPos;
[SerializeField] private Transform pos1, pos2, pos3, pos4, pos5, pos6;
[SerializeField] private float speed;
[SerializeField] private float startWaitTime;
[SerializeField] private float waitTime;
[SerializeField] private Transform[] randomPos;
int pickRandomPos;
private bool facingRight = true;
private Vector3 nextPos;
void Awake()
{
nextPos = startPos.position;
pickRandomPos = Random.Range(0, randomPos.Length);
}
void Update()
{
if (transform.position == randomPos[pickRandomPos].position)
{
if (waitTime <= 0) { nextPos = randomPos[pickRandomPos].position; waitTime = startWaitTime; Flip(); }
else { waitTime -= Time.deltaTime; }
}
else if (transform.position == randomPos[pickRandomPos].position)
{
if (waitTime <= 0) { nextPos = randomPos[pickRandomPos].position; waitTime = startWaitTime; Flip(); }
else { waitTime -= Time.deltaTime; }
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
I added an array and implemented the randomness logic but it didn’t work for me because I cant figure out why! honestly I understand the concept but I think I’m not implementing it correctly.
After so many attempts and reads i gave up on that code and decided to watch a tutorial and ended up with the following code:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : MonoBehaviour
{
[SerializeField] private float currentSpeed;
[SerializeField] private float startWaitTime;
[SerializeField] private float waitTime;
[SerializeField] private Transform[] moveSpots;
private int randomSpot;
void Start()
{
randomSpot = Random.Range(0, moveSpots.Length);
//transform.LookAt(moveSpots[randomSpot]);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, currentSpeed * Time.deltaTime);
if (Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2)
{
if(waitTime <= 0)
{
randomSpot = Random.Range(0, moveSpots.Length);
//transform.LookAt(moveSpots[randomSpot]);
waitTime = startWaitTime;
}
else { waitTime -= Time.deltaTime; }
}
}
}
I added a few Positions to the array and the code worked pretty well but unfortunately I didn’t know how to implement the Flip() function into that no matter what I did. The only semi successful attempt was playing around the Sprite Renderer Flip X bool but it only works when the Enemy is literally positioned on the 0 X axis so its really not a good solution.
My request is for you to help me to make the first code pick a Position randomly or help me make my Character flip its dumb face in the last code.
PS. I’ve tried to use LookAt() functionality but it made my character’s Sprite disappear for unknown reasons.
I appreciate your time and effort!
looking forward to read your suggestions.