Collision based on array of enemies

Hello all,

New to C# here, any programming language on that note. I am teaching myself C# by making a little game and found myself running into an issue. I have created an array of enemies at the beginning of my code, when a play collides with an enemy, the play is knocked back a short distance (Currently using around 8 force). This is working great when there is only a single enemy, however if add more the one enemy, it will base the collision script on what ever the highest enemy is within the array.

I am using loop that will looking for enemies in the array but can’t seem to get this fixed. Any help would be most appreciated. Here is my script below;

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour 
{ 
	public float maxSpeed = 10f;	// Character movement speed.
	public bool grounded = false; 	//is the Character grounded?
	public Transform groundCheck; 	// Used for creating grounded 
	float groundRadius = 0.2f;		//
	public LayerMask whatIsGround;	// Marker to show where the ground is
	float jumpForce = 15f;			// Force for vertical velocity (jumping)
	float knockForce = 8f;			// Force which the character will be knocked on both axis		
	float travelTime = 0.1f;		// Timer to set how often the timer will check for grounded to reset velocity
	public float doubleJump = 1;	// Double jump limit
	bool movement = true;			// Disables movement during knockbacks
	public int enemyIndex = 0; 			// Used to check array each from in a enemyIndex++
	Vector3 enemy_pos;

	private GameObject[] Enemy;

	void Start(){
		Enemy = GameObject.FindGameObjectsWithTag ("Enemy");	// Define what is Enemy using tag Enemy 
		enemy_pos = Enemy[enemyIndex].transform.position; // Define the position of the Enemy array

	}


	void Update(){
		enemyPosition ();


		grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);	// Checks for grounded		

		if (movement) {

		// Character Movement
		if (Input.GetKey (KeyCode.A)) {
			transform.position += Vector3.left * maxSpeed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.D)) {
			transform.position += Vector3.right * maxSpeed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.W)) {				
			transform.position += Vector3.up * maxSpeed * Time.deltaTime;
		}
		if (Input.GetKey (KeyCode.S)) {
			transform.position += Vector3.down * maxSpeed * Time.deltaTime;
		}
		}

		// Jumping 
		if (grounded && Input.GetButtonDown ("Jump")) {
			rigidbody2D.velocity = new Vector2 (0, jumpForce);
		}

		// Double jump
		if (!grounded && Input.GetButtonDown ("Jump") && doubleJump > 0) {
			rigidbody2D.velocity = Vector2.zero;
			rigidbody2D.velocity = new Vector2 (0, jumpForce);
			doubleJump = 0;
				} else if (grounded == true) {		
					doubleJump = 1;
				}
	}

	void OnCollisionStay2D(Collision2D coll) {
		if (coll.gameObject.tag == "Enemy") {
		//	rigidbody2D.velocity = new Vector2 (-8, 8); -- Old Code
			rigidbody2D.velocity = (knockForce * (transform.position - enemy_pos));
			Debug.Log (knockForce * (transform.position - enemy_pos));
			StartCoroutine (knockBack (travelTime));
			movement = false;

		}
	
	}

	IEnumerator knockBack(float travelTime) {
		yield return new WaitForSeconds (travelTime);
		movement = true;
		if (!grounded) {
			StartCoroutine (knockBack (travelTime));
			movement = false;
		}
		if (grounded){
			rigidbody2D.velocity = Vector3.zero;
		}
	}

	void enemyPosition(){
		enemyIndex++;
		if (enemyIndex >= Enemy.Length);
		Debug.Log (Enemy.Length);
			enemyIndex = 0;
		}

}

Please don’t mind the pretty messy code. Will clean that up as I near the end of this particular script!

1 Answer

1

For those who get lost like I did - I over complicated it. Resolve the issue with this.

	void OnCollisionStay2D(Collision2D coll) {
		if (coll.gameObject.tag == "Enemy") {
			rigidbody2D.velocity = (knockForce * (transform.position - coll.gameObject.transform.position));
			Debug.Log (knockForce * (transform.position - coll.gameObject.transform.position));
			StartCoroutine (knockBack (travelTime));
			movement = false;

		}