Search Unity

Follow motion with delay and collisions.

Discussion in 'Getting Started' started by Krzym, Nov 5, 2022.

  1. Krzym

    Krzym

    Joined:
    Jan 27, 2018
    Posts:
    1
    Hi, Just started learning Unity and I'd like to create snake-like movements with collisions.
    I'd appreciate any tips on how to solve that, for now, I managed to create follow movement, but I have issues with keeping the initial distance and activating collisions.
    Adding just a rigid body to the cubes does not work very smoothly.

    Important for me is to keep the length of a snake as procedural as possible.

    Here's a preview of what I have:
    https://www.dropbox.com/s/m0s5eok3l2gihzj/unity.mp4?dl=0

    Didn't find any good answer about that, maybe I should ray-cast test the collisions? or generate some kind of joint hierarchy instead of separate objects?

    and some code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BodyGenerator : MonoBehaviour
    6. {
    7.     public float bodyParts = 1;
    8.     public Vector3 bodyPartSize;
    9.     public int Gap = 10;
    10.     public GameObject petMain;
    11.     public PlayerController pc;
    12.  
    13.     // Lists
    14.     private List<GameObject> BodyParts = new List<GameObject>();
    15.     private List<Vector3> PositionsHistory = new List<Vector3>();
    16.  
    17.     void Start() {
    18.         CreateBody();
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         // if (pc.isMoving == true) {
    25.         PositionsHistory.Insert(0, gameObject.transform.position);
    26.  
    27.         int index = 0;
    28.         foreach (var body in BodyParts) {
    29.             Vector3 point = PositionsHistory[Mathf.Clamp(index * Gap, 0, PositionsHistory.Count -1)];
    30.             Vector3 moveDirection = point - body.transform.position;
    31.             body.transform.position += moveDirection * 5 * Time.deltaTime;
    32.             body.transform.LookAt(point);
    33.             index++;
    34.         }
    35.  
    36.         if (PositionsHistory.Count > 200) {
    37.             PositionsHistory.RemoveAt(PositionsHistory.Count - 1);
    38.         }
    39.         // }
    40.  
    41.  
    42.        
    43.     }
    44.  
    45.     private void CreateBody() {
    46.         GameObject body = new GameObject("Body_grp");
    47.         body.transform.parent = petMain.transform;
    48.  
    49.         Vector3 position = new Vector3(0, 0, 0);
    50.         GameObject parent = body;
    51.  
    52.         for (int i=0; i <= bodyParts; i++) {
    53.             GameObject bodyCube = CreateCube(bodyPartSize, position, parent);
    54.             position = position - new Vector3(0,0,bodyPartSize.z);
    55.             // parent = bodyCube;
    56.             BodyParts.Add(bodyCube);
    57.         }
    58.  
    59.     }
    60.  
    61.     private GameObject CreateCube(Vector3 scale, Vector3 postition, GameObject parent)  {
    62.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    63.         cube.transform.localScale = scale;
    64.         cube.transform.position = postition;
    65.         cube.transform.parent = parent.transform;
    66.  
    67.         return cube;
    68.     }
    69. }
    70.  
    Thanks in advance.