Reputation: 1
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 first 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;
}
}
}
Upvotes: 0
Views: 70
Reputation: 144
As soon as you have multiple platforms, at least one will always set the parent transform to null(since you can at most have one platform below the player, i assume), meaning that even if you stand on a platform it's a race condition for which platform updates first in the loop, as platform #2 and platform #3 will try to set the parent to null.
A fix to this would be anchoring the player to the platform within the trigger methods, rather than within the update method, so that each platform only cares about what happens when the player enters/exits that specific platform, and not what happens in the rest of the game world.
I don't know your exact setup though, so my approach might not be the correct way to handle it for your scenario, but just throwing it out there.
Upvotes: 1