So I’m working right now on my game for my School and I encountered a weird problem.
Explaining what I want:
The player is consistantly going to the right on the X axis (without pressing anything), enemy are spawning in front og him and coming towards the player (they are moving slowly towards -x), the player has to shoot them before they get behind him, or the player would lose a life. In front of the player is a Trigger (gameobject) which destroys the bullets shot, which didn’t hit anything (so there are not too many objects in the scene at any point). Behind the player is another Trigger (gameobject) which should trigger the enemy to destroy the enemy and substract a life, but I can’t get that part working…
Infos:
- The Main Camera is follwing the player Position via script
- the “BulletDestroyer” GameObject (which destroys the bullets infront of the player) is a child of the player, so it will be always infront of the player
- The player can switch “lanes” by pressing W and S which is chainging the sorting layer on the player (works perfectly fine)
What I tried so far:
- Make the “BankObj” GameObject (which is the Trigger) a child of the player like the “BulletDestroyer” → My Game glitches out then, the player won’t move anymore and sometimes is moving rapidly back or forward…
- Make the “BankObj” a child of the camera
- Giving the “BankObj” a script where the GameObject is following the player position
- Changing that script, so the “BankObj” is pushing itself forward like the player
Script on the Enemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VillianScript_WildWest : MonoBehaviour {
public int health = 10;
public GameObject[] healthHearts;
public int dmg = 1;
public int speed = 2;
private void Start()
{
healthHearts[1].transform.position = new Vector3(transform.position.x, transform.position.y + 15, 0);
}
private void OnTriggerEnter2D(Collider2D col)
{
print("Kein Treffer!");
if (col.CompareTag("Bank"))
{
print("Treffer!");
col.SendMessage("TakeDamage", dmg, SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
}
private void FixedUpdate()
{
transform.Translate(-speed * Time.deltaTime, 0, 0f, Space.World);
}
public void TakeDamage(int damage)
{
health -= damage;
if (health == 10)
{
healthHearts[1].SetActive(true);
}
if (health < 10)
{
healthHearts[1].SetActive(false);
healthHearts[0].transform.position = new Vector3(transform.position.x, transform.position.y + 15, 0);
healthHearts[0].SetActive(true);
}
if (health <= 0)
{
healthHearts[0].SetActive(false);
GameObject enemyCtr = GameObject.Find("EnemyController");
EnemyController enem = enemyCtr.GetComponent<EnemyController>();
enem.enemysLeft++;
Destroy(gameObject);
}
}
}
Script on the BankObj
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BankPosScript : MonoBehaviour {
public Transform player;
Transform yourPos;
int speed = 4;
// Update is called once per frame
void FixedUpdate () {
//gameObject.transform.position = new Vector2(player.position.x - 40f, 0);
transform.Translate(speed * Time.deltaTime, 0, 0f, Space.World);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Villian"))
{
Destroy(collision.gameObject);
}
}
}
The “BankObj” has the Tag “Bank” and the Enemy has the Tag “Villian”
If the enemy collides with the player on its way to the left, the OnTriggerEnter2D is triggered and is writing into my console “Kein Treffer!” but it won’t trigger with the “BankObj”
I’m frustrating on that problem…