A
A
Alexander Fandeev2021-02-20 00:06:00
C++ / C#
Alexander Fandeev, 2021-02-20 00:06:00

How to use the OnTriggerEnter() function on multiple game objects?

Good day everyone. There is a game prototype that replicates CubeSurfer's play style. The logic is this - you control the cube, pick up certain objects like a snake fruit, due to which the number of cubes under your feet grows. At the moment, I'm trying to make the player, when colliding with an object collider, just create this cube under the "legs". And the whole problem is that there are three such "collectible" items in the game so far. The player can take only and only one. He doesn't respond to anything else. That is, if you take the first, second or third, the code will work, but then it just skips it. I tried to improvise with RigidBody, but it didn't work out. The player simply flies into the stratosphere upon contact with any object, or creates a cube within a cube, which should not be. Help with advice how to solve this by no means simple (or simple) problem. Here is the complete code overlaid on the three game objects:

spoiler

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCollider : MonoBehaviour
{

[SerializeField] GameObject destroyIt;
[SerializeField] GameObject objToClone;
[SerializeField] Transform rootObjInScene;


Transform curParent;
Vector3 posOffset, posPlayer;
float distance = 1f;

private void OnTriggerStay(Collider player)
{
PlayerJumping();
CreateAnother();
Destroy(destroyIt);
}

public void PlayerJumping()
{
posPlayer = Vector3.up * distance;
Vector3 playerPos = rootObjInScene.position + posPlayer;
rootObjInScene.transform.position = playerPos;
}

public void Awake()
{
curParent = rootObjInScene;
posOffset = Vector3.down * distance;
}

public void CreateAnother()
{
Vector3 newPos = curParent.position + posOffset;
GameObject newObj = Instantiate(objToClone, newPos, Quaternion.identity, curParent);

curParent = newObj.transform;
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igorloadgame, 2021-02-21
@alexandrfandeev

Look, when the triggers touch, raise the man himself (there was one in the game itself, like you don’t know) and an array of cubes and create a copy of the cube from the very bottom, and if the cube is lost, take away the cube and lower the man and the array of cubes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question