How do I make a GameObject move towards another GameObject but stay within collider bounds of another GameObject?

Viewed 37

I'm working on a Boss for my game, here's an image:

1

The selected collider is attached to the Pupil of the Eye of the boss, here's a pic of both colliders showing:

2

I also have a Player Objec.

3

The Boss is a floating All Seeing Eye that always moves towards the player.

What I'm trying to do is make the Pupil move separately from the Boss while still staying within the collider bounds I've made, and always move towards the player to give the illusion that the eye is always looking at the player. I tried using Vector2.MoveTowards() but it didn't work.

How might I solve this problem? Help would be appreciated thanks

Here's the current script attached to the Boss Object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AllSeeingEyeController : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private GameObject myEye; // Accesses the Pupil GameObject of the Boss
    private Transform player;
    private bool spawningEyeballs = false;
    private Rigidbody2D rb;
    private Rigidbody2D myEyeRb; // Acesses the Pupil's rigidbidy2d, since that's how I think it should move

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        rb = GetComponent<Rigidbody2D>();
        myEyeRb = myEye.GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if (!spawningEyeballs) {
            ChasePlayer();
        } else {
            rb.velocity = Vector2.zero;
        }
    }
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player")) {
            DedaController2D main = other.gameObject.GetComponent<DedaController2D>();
            main.knockbackCount = main.knockbackLength; //Activates Knockback
            if (transform.position.x > other.transform.position.x) { //Player is Left of Enemy, Enemy knocks from Right
                main.knockFromRight = true;
                main.knockUpwards = true;
            } else { //Player is Right of Enemy, Enemy knocks from Left
                main.knockFromRight = false;
                main.knockUpwards = true;
            }
        }
    }
    void ChasePlayer()
    {
        if (transform.position.x < player.position.x) { //Player is Right of Enemy, Enemy goes Right
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
            transform.localScale = new Vector2(-1, 1);
        } else if (transform.position.x > player.position.x){ //Player is Left of Enemy, Enemy goes Left
            rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
            transform.localScale = new Vector2(1, 1);
        } else {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
        if (transform.position.y < player.position.y) { //Player is Above Enemy, Enemy goes Up
            rb.velocity = new Vector2(rb.velocity.x, moveSpeed / 2);
        } else if (transform.position.y > player.position.y) { //Player is Below Enemy, Enemy goes Down
            rb.velocity = new Vector2(rb.velocity.x, -moveSpeed / 2);
        } else {
            rb.velocity = new Vector2(rb.velocity.x, 0);
        }
    }
}
0 Answers
Related