I’m making a game where the player’s movement is bound by a second screen inside the main screen.
The player is the black and white square at the center of the screen and the enemy, in this case, is the golden key at the corner.
I want to make the enemy appear from a random corner of the main screen and move in a straight or diagonal line to another random corner, where it will disappear and be destroyed.
The thing is, I need to make sure that during its movement, the enemy steps into the player’s boundaries, so that the player has the chance of interacting with it. How can I pull this off?
Here are the scripts I’ve made so far:
PlayerMovement.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private float rotation;
public Rigidbody2D rb;
public float xStartPos;
public float yStartPos;
// Start is called before the first frame update
void Start()
{
speed = 4.0f;
rotation = 1.5f;
}
// Update is called once per frame
void Update()
{
//Project settings using WSAD for Input Axis instead of Arrow Keys
float xMov = Input.GetAxisRaw("Horizontal");
float yMov = Input.GetAxisRaw("Vertical");
rb.velocity = new Vector2(xMov * speed, yMov * speed);
if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(new Vector3(0,0,1), -rotation);
}
if(Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(new Vector3(0,0,1), rotation);
}
}
}
Boundaries.cs (for the player):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boundaries : MonoBehaviour
{
[SerializeField] private float xBound;
[SerializeField] private float yBottomBound;
[SerializeField] private float yTopBound;
void Start(){
xBound = 4.05f;
yBottomBound = 2.6f;
yTopBound = 2.85f;
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -xBound, xBound),
Mathf.Clamp(transform.position.y, -yBottomBound, yTopBound), transform.position.z);
}
}
Enemy.cs (I was trying to come up with something here to no avail):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector2 limit = new Vector2(Random.Range(-4.05f, 4.05f), Random.Range(-2.6f, 2.85f));
//mudar o transform.position dentro do MoveTowards para algo que represente o centro da tela
transform.position = Vector2.MoveTowards(transform.position, limit, );
/*
O inimigo vai girando numa direção que esteja dentro dos
limites do jogador.
Ele surge em um canto aleatório da tela, pequeno e escurecido,
ficando maior e mais claro conforme se aproxima do centro da
tela.
*/
}
}
