Search Unity

Snake vs blocks snake movement!

Discussion in 'Scripting' started by Nishchhal, Nov 22, 2017.

  1. Nishchhal

    Nishchhal

    Joined:
    Sep 19, 2017
    Posts:
    9
    I am working on figuring the movement of snake vs blocks game from about 20 days, i succeded to make a smooth follow of tail but the problem is the gap in between the tail pieces when i try to move the snake fast from one place to another.
    Need Help!
     
  2. wayne1512

    wayne1512

    Joined:
    Oct 23, 2017
    Posts:
    84
    @Nishchhal If you don't mind copying the script that makes the snake move maybe I can help.
    btw is is C# or Java because if java i don't know
     
  3. Nishchhal

    Nishchhal

    Joined:
    Sep 19, 2017
    Posts:
    9
    Sor
    Sorry for the late reply here is the script.




      1. using UnityEngine;
      2. using System.Collections;
      3. using System.Collections.Generic;
      4. using System.Linq;

      5. public class Snake : MonoBehaviour
      6. {
      7. public Transform head;
      8. public Transform[] segments;
      9. List<Vector3> breadcrumbs;

      10. public float segmentSpacing; //set controls the spacing between the segments,which is always constant.

      11. void Start()
      12. {
      13. //populate the first set of crumbs by the initial positions of the segments.
      14. breadcrumbs = new List<Vector3>();
      15. breadcrumbs.Add(head.position); //add head first, because that's where the segments will be going.
      16. for (int i = 0; i < segments.Length; i++) // we have an extra-crumb to mark where the last segment was...
      17. breadcrumbs.Add(segments.position);
        [*] }
        [*]

        [*] void Update()
        [*] {
        [*]

        [*] float headDisplacement = (head.position - breadcrumbs[0]).magnitude;
        [*]

        [*] if (headDisplacement >= segmentSpacing)
        [*] {
        [*] breadcrumbs.RemoveAt(breadcrumbs.Count - 1); //remove the last breadcrumb
        [*] breadcrumbs.Insert(0, head.position); // add a new one where head is.
        [*] headDisplacement = headDisplacement%segmentSpacing;
        [*] }
        [*]

        [*] if (headDisplacement != 0)
        [*] {
        [*] Vector3 pos = Vector3.Lerp(breadcrumbs[1], breadcrumbs[0], headDisplacement / segmentSpacing);
        [*] segments[0].position = pos;
        [*] segments[0].rotation = Quaternion.Slerp(Quaternion.LookRotation(breadcrumbs[0] - breadcrumbs[1]), Quaternion.LookRotation(head.position - breadcrumbs[0]), headDisplacement / segmentSpacing);
        [*] for (int i = 1; i < segments.Length; i++)
        [*] {
        [*] pos = Vector3.Lerp(breadcrumbs[i + 1], breadcrumbs, headDisplacement / segmentSpacing);
        [*] segments.position = pos;
        [*] segments.rotation = Quaternion.Slerp(Quaternion.LookRotation(breadcrumbs - breadcrumbs[i + 1]), Quaternion.LookRotation(breadcrumbs[i - 1] - breadcrumbs), headDisplacement / segmentSpacing);
        [*] }
        [*] }
        [*]

        [*] }
        [*]}




    The thread that i took refrence from is

    https://forum.unity.com/threads/tail-follow-script.81635/
     
  4. Nishchhal

    Nishchhal

    Joined:
    Sep 19, 2017
    Posts:
    9
    Eagerly looking for help on this topic. Any help will be very helpful.
    Thanks in Advance.