T
T
TonyMatony2020-10-27 20:14:59
C++ / C#
TonyMatony, 2020-10-27 20:14:59

How to solve the problem with the passage of objects through each other?

At the start of the game, all objects enter each other and continue to move together, and should move one after another.
5f9854e4d4aa1768096558.png5f9854ee3db84903560157.png

The 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 decide whether the issue is in the code or needs to be configured in the engine itself?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DanielMcRon, 2020-10-27
@TonyMatony

I haven't looked at the code yet, but I saw that you have sphere colider disabled. Turn them on for every object

S
stalker1hunt, 2020-11-03
@stalker1hunt

Offtopic, but
PlayerCintroller
Smiled)
Oh yes, I see you are not using physics for movement, but transforms.
And if you use the body, then be kind enough to move them with physics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question