jeudi 5 août 2021

Why are both if-Statements seperatly working, but together they don't?

I guess this is a really easy problem, but I just started Programming. I tried to Set the Player as a Child of a Platform while they touch, so he is moving with it. And if there is only one such Platform in the Game it works, but otherwise, the Player can only stick on the Platform I placed last. On the otherones, both bools isOnBlock and findPlayer are set true (I tested this), but the If-Statement still doesn`t work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HaftungAnBlock : MonoBehaviour
{

public bool isOnBlock;
private GameObject myPlayer;
bool findPlayer;


public void FindPlayer()
{
    myPlayer = GameObject.FindGameObjectWithTag("Player");
    findPlayer = true;
}
void Update()
{
    if (isOnBlock && findPlayer)
    {
            myPlayer.transform.SetParent(this.transform);
    } 
    else if (!isOnBlock && findPlayer)
    {
        myPlayer.transform.SetParent(null);
    }
}

private void OnCollisionEnter(Collision other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        isOnBlock = true;
    }
}
private void OnCollisionExit(Collision other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        isOnBlock = false;
    }
}

}

Aucun commentaire:

Enregistrer un commentaire