Question by
GeorgeGiap · May 22, 2018 at 05:09 PM ·
gameobjectscript.destroy
One script for multiple enemy objects problem
Here is my problem. I am making a 2D platformer super mario bros clone. I have 2 enemies in the scene and one script is attached to both of them. If i step on one of them the other one is getting destroyed. What can i do to solve this problem?
Here is the EnemyController script that extends the RaycastController class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : RaycastController
{
public float moveSpeed = -30;
[HideInInspector]
public Vector2 velocity = Vector2.zero;
private float gravity = -30;
private float directionX = 1;
private float directionY;
private Rigidbody2D rb2d;
private Animator animator;
private AudioManager audioManager;
// Use this for initialization
void Start()
{
Time.timeScale = 1; // restore time scale from before pause
Debug.Log("THIS particular script is on " + this.name);
base.Start();
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
audioManager = FindObjectOfType<AudioManager>();
UpdateRaycastOrigins();
}
// Update is called once per frame
void Update()
{
UpdateRaycastOrigins();
HorizontalCollisions();
VerticalCollisions();
rb2d.velocity = new Vector2(velocity.x, -velocity.y);
}
void HorizontalCollisions()
{
float rayLength = 1.5f;
for (int i = 5; i < horizontalRayCount - 42; i++)
{
Vector2 rayOriginLeft = raycastOrigins.bottomLeft;
Vector2 rayOriginRight = raycastOrigins.bottomRight;
rayOriginLeft += Vector2.up * (horizontalRaySpacing + 1 * i + skinWidth);
rayOriginRight += Vector2.up * (horizontalRaySpacing + 1 * i + skinWidth);
RaycastHit2D hitLeft = Physics2D.Raycast(rayOriginLeft, Vector2.left, rayLength, collisionMask);
RaycastHit2D hitRight = Physics2D.Raycast(rayOriginRight,Vector2.right, rayLength , collisionMask);
Debug.DrawRay(rayOriginLeft, Vector2.left, Color.red);
Debug.DrawRay(rayOriginRight, Vector2.right , Color.red);
if (hitLeft)
{
directionX = -directionX;
rayLength = hitLeft.distance;
if (hitLeft.collider.tag == "Player")
{
if (!audioManager.audio.isPlaying)
audioManager.PlayAudio("Die");
StartCoroutine(Pause());
PlayerLose();
}
}
if (hitRight)
{
directionX = -directionX;
rayLength = hitRight.distance;
if (hitRight.collider.tag == "Player")
{
if (!audioManager.audio.isPlaying)
audioManager.PlayAudio("Die");
StartCoroutine(Pause());
PlayerLose();
}
}
velocity.x = moveSpeed * directionX;
}
}
void VerticalCollisions()
{
float rayLength = 1.5f;
for (int i = 0; i < verticalRayCount-39; i++)
{
Vector2 rayOriginDown = raycastOrigins.bottomLeft;
Vector2 rayOriginUp = raycastOrigins.topLeft;
rayOriginDown += Vector2.right * (verticalRaySpacing + 1 * i + skinWidth);
rayOriginUp += Vector2.right * (verticalRaySpacing + 1 * i + skinWidth);
RaycastHit2D hitDown = Physics2D.Raycast(rayOriginDown, Vector2.down, rayLength , collisionMask);
RaycastHit2D hitUp = Physics2D.Raycast(rayOriginUp, Vector2.up, rayLength , collisionMask);
Debug.DrawRay(rayOriginDown, Vector2.down, Color.red);
Debug.DrawRay(rayOriginUp, Vector2.up , Color.red);
if (hitDown)
{
velocity.y = 0;
rayLength = hitDown.distance;
if(hitDown.collider.tag == "Player")
{
if(!audioManager.audio.isPlaying)
audioManager.PlayAudio("Die");
StartCoroutine(Pause());
PlayerLose();
}
}
else
{
velocity.y -= gravity * Time.deltaTime;
}
if (hitUp)
{
if(hitUp.collider.tag == "Player")
{
velocity.x = 0;
}
velocity.y = 0;
rayLength = hitUp.distance;
}
}
}
public void EnemyDeath()
{
animator.Play("Dead");
animator.SetBool("Crushed", true);
audioManager.PlayAudio("Squish");
Destroy(this.gameObject);
Destroy(this.rb2d);
Destroy(this.collider);
Destroy(this.gameObject, 1f);
}
IEnumerator Pause()
{
Time.timeScale = 0; // pause
yield return null;
}
void PlayerLose()
{
Application.LoadLevel("Lose");
}
}
`
And here is the RaycastController script:
`
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (BoxCollider2D))]
public class RaycastController : MonoBehaviour {
public struct RaycastOrigins
{
public Vector2 topLeft, topRight;
public Vector2 bottomLeft, bottomRight;
}
public RaycastOrigins raycastOrigins;
public LayerMask collisionMask;
protected BoxCollider2D collider;
protected const float skinWidth = .015f;
protected int horizontalRayCount;
protected int verticalRayCount;
protected float horizontalRaySpacing;
protected float verticalRaySpacing;
private const float dstBetweenRays = .25f;
public virtual void Awake() {
collider = GetComponent<BoxCollider2D> ();
}
public virtual void Start() {
CalculateRaySpacing ();
}
public void UpdateRaycastOrigins() {
Bounds bounds = collider.bounds;
bounds.Expand (skinWidth * -2);
raycastOrigins.bottomLeft = new Vector2 (bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2 (bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2 (bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2 (bounds.max.x, bounds.max.y);
}
public void CalculateRaySpacing() {
Bounds bounds = collider.bounds;
bounds.Expand (skinWidth * -2);
float boundsWidth = bounds.size.x;
float boundsHeight = bounds.size.y;
horizontalRayCount = Mathf.RoundToInt (boundsHeight / dstBetweenRays);
verticalRayCount = Mathf.RoundToInt (boundsWidth / dstBetweenRays);
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
}
Comment
Answer by GeorgeGiap · May 23, 2018 at 07:20 AM
Why not? This is just a project for learning purposes and nothing else. Is there any way that i can solve this problem? Or i would be grateful if you can suggest any other way of doing it. If you know please share it with me. Thanks.