Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sharing script for sci-fi worm behavior

Discussion in 'Scripting' started by Kennai, Jan 4, 2021.

  1. Kennai

    Kennai

    Joined:
    Nov 1, 2018
    Posts:
    27
    Hey guys!
    I was searching for a DumpedTranform script to animate my worm, and was dissapointed when found that code. So I made my own script, maybe it would be useful for someone :)
    this script do this


    My script settings:


    Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WormTransform : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private List<Transform> transformList;
    9.  
    10.     [SerializeField]
    11.     private float distance = 1.5f;
    12.  
    13.     [SerializeField, Range(0f, 50f)]
    14.     private float moveSpeed = 10f;
    15.  
    16.     [SerializeField, Range(0f, 50f)]
    17.     private float rotateSpeed = 10f;
    18.  
    19.     private List<WormCell> cellList = new List<WormCell>();
    20.     private float deltaMove, deltaRotate;
    21.  
    22.     private void Awake()
    23.     {
    24.         for (int i = 0; i < transformList.Count - 1; i++)
    25.         {
    26.             cellList.Add(new WormCell(transformList[i], transformList[i + 1]));
    27.         }
    28.     }
    29.  
    30.     private void OnEnable()
    31.     {
    32.         for (int i = 0; i < cellList.Count; i++)
    33.             cellList[i].Init(distance);
    34.     }
    35.  
    36.     private void FixedUpdate()
    37.     {
    38.         int i;
    39.  
    40.         deltaMove = moveSpeed * Time.fixedDeltaTime;
    41.         deltaRotate = rotateSpeed * Time.fixedDeltaTime;
    42.  
    43.         //for (i = cellList.Count-1; i >= 0; i--)
    44.         for (i = 0; i < cellList.Count; i++)
    45.             cellList[i].Calculate(deltaMove, deltaRotate, distance);
    46.  
    47.         for (i = 0; i < cellList.Count; i++)
    48.             cellList[i].Apply();
    49.     }
    50.  
    51. #if UNITY_EDITOR
    52.     private void LateUpdate()
    53.     {
    54.         for (int i = 0; i < cellList.Count; i++)
    55.             cellList[i].Apply();
    56.     }
    57. #endif
    58. }
    59.  
    60. public class WormCell
    61. {
    62.     private Transform source, target;
    63.  
    64.     private Quaternion rotation;
    65.     private Vector3 position;
    66.  
    67.     public WormCell(Transform source, Transform target)
    68.     {
    69.         this.source = source;
    70.         this.target = target;
    71.     }
    72.  
    73.     public void Init(float distance)
    74.     {
    75.         target.position = source.position - source.forward * distance;
    76.         target.rotation = source.rotation;
    77.         rotation = target.rotation;
    78.         position = target.position;
    79.     }
    80.  
    81.     public void Calculate(float deltaMove, float deltaRotate, float distance)
    82.     {
    83.         rotation = Quaternion.Lerp(rotation, source.rotation, deltaRotate);
    84.         position = source.position - target.forward * distance;
    85.     }
    86.  
    87.     public void Apply()
    88.     {
    89.         target.rotation = rotation;
    90.         target.position = position;
    91.     }
    92. }
    Enjoy!

    p.s.
    if movement would be laggy, remove #if & #endif lines
     
    alexeu, cangkts and PraetorBlue like this.
  2. cangkts

    cangkts

    Joined:
    Mar 5, 2020
    Posts:
    6
    Hello, i want the cubes to follow the head with nearly 0 distance. but when i do that, the body doesnt seems like moving in one piece. So i dont want to see other cubes, i just want to see them moving like a one piece . what do i need to do? more cubes? close distance? maybe some changes in the source code ?
     
  3. Kennai

    Kennai

    Joined:
    Nov 1, 2018
    Posts:
    27
    Hi, are you sure you need 0 distance? maybe you need distance equal to cube width? so there wont be "empty spaces"
    Or maybe you can draw or show what you need exactly?
     
  4. cangkts

    cangkts

    Joined:
    Mar 5, 2020
    Posts:
    6
    Its like this rn. i want it like its one piece cube not 10 pieces come together
     

    Attached Files:

  5. Kennai

    Kennai

    Joined:
    Nov 1, 2018
    Posts:
    27
    hmm, I suppose you need 2 cells then, not 10. 2 cells - its head + cube. You can use "dummy" head, so head wont be a cube. Just empty game object. and it will have second cell - cube. second cell will follow head. Is it what you need?
     
  6. cangkts

    cangkts

    Joined:
    Mar 5, 2020
    Posts:
    6
    Unfortunately not. Maybe this picture will help.

    Edit: i figured out if i use bones instead of cubes it worked like i wanted to. Kennai if its possible to make it with cubes please let me know. Thank you.
     

    Attached Files:

    Last edited: Jun 17, 2021
  7. Kennai

    Kennai

    Joined:
    Nov 1, 2018
    Posts:
    27
    Well, my script definitelly is not what you need. I think you need Joints, which works together with physics
     
  8. cangkts

    cangkts

    Joined:
    Mar 5, 2020
    Posts:
    6
    I will look into that also. Thanks again.
     
  9. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Nice! Thanks for sharing. works pretty good.
    You can have a nice elastic effect by Lerping the position, using a multiple of deltaMove as t.

    Bravo.