menu

arrow_back How to solve the problem with objects passing through each other?

by
2 votes
At the start of the game all objects enter each other and continue moving together, and the must move one after the other.
5f9854e4d4aa1768096558.png5f9854ee3db84903560157.png


Code
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;

public class PlayerCintroller : MonoBehaviour
{
[HideInInspector]
public PlayerDirection direction;

[HideInInspector]
public float step_Length = 1f; //???

[HideInInspector]
public float movement_Frequensy = 0.2f;

private float counter;
private bool move;

[SerializeField]
private GameObject tailPrefab;

private List<Vector3> delta_Position;

private List<Rigidbody> nodes;

private Rigidbody main_Body;
private Rigidbody head_Body;
private Transform tr;

private bool createNodeAtTail;




void Awake()
{
tr = transform;
main_Body = GetComponent<Rigidbody>();

InitSnakeNodes();
InitPlayer();

delta_Position = new List<Vector3>
{
new Vector3(-step_Length,0f), //1
new Vector3(0f, step_Length), //2
new Vector3(step_Length, 0f), //3
new Vector3(0f, -step_Length),//4
};
}


void Update()
{
CheckMovementFrequency();
}

void FixedUpdate()
{
if (move)
{
move = false;
Move();
}
}

void InitSnakeNodes()
{
nodes = new List<Rigidbody>();

nodes.Add(tr.GetChild(0).GetComponent<Rigidbody>());
nodes.Add(tr.GetChild(1).GetComponent<Rigidbody>());
nodes.Add(tr.GetChild(2).GetComponent<Rigidbody>());

head_Body = nodes[0];
}

void SetDirectionRandom()
{
int dirRandom = Random.Range(0, (int)PlayerDirection.COUNT);
direction = (PlayerDirection)dirRandom;
}

void InitPlayer()
{
SetDirectionRandom();
switch (direction)
{
case PlayerDirection.RIGHT:
nodes[1].position = nodes[0].position - new Vector3(Metrics.NODE, 0f, 0f);
nodes[2].position = nodes[0].position - new Vector3(Metrics.NODE * 2f, 0f, 0f);
break;
case PlayerDirection.LEFT:
nodes[1].position = nodes[0].position + new Vector3(Metrics.NODE, 0f, 0f);
nodes[2].position = nodes[0].position + new Vector3(Metrics.NODE * 2f, 0f, 0f);
break;
case PlayerDirection.UP:
nodes[1].position = nodes[0].position - new Vector3( 0f, Metrics.NODE, 0f );
nodes[2].position = nodes[0].position - new Vector3( 0f, Metrics.NODE * 2f, 0f );
break;
case PlayerDirection.DOWN:
nodes[1].position = nodes[0].position + new Vector3(0f, Metrics.NODE, 0f);
nodes[2].position = nodes[0].position + new Vector3(0f, Metrics.NODE * 2f, 0f);
break;
}


}
void Move()
{
Vector3 dPosition = delta_Position[(int)direction];

Vector3 parentPosition = head_Body.position;
Vector3 prevPosition;


head_Body.position = head_Body.position + dPosition;
main_Body.position = main_Body.position + dPosition;

for (int i = 1; i < nodes.Count; i++)
{
prevPosition = nodes[i].position;

nodes[i].position = parentPosition;
parentPosition = prevPosition;
}
if (createNodeAtTail)
{

}

}

void CheckMovementFrequency()
{
counter += Time.deltaTime;
if (counter >= movement_Frequensy)
{
counter = 0f;
move = true;
}
}
}

How to solve it, is the issue in the code or need to adjust the engine itself?

2 Answers

by
 
Best answer
0 votes
I haven't looked at the code yet, but I see that you have disabled sphere coliders. Turn them on for each object.
by
0 votes
Offtop, but
PlayerCintroller
(Laughs)

Ah yes, I see that you do not use physics to move, but trasform.
And if you're using your body, be kind enough to move them with physics.