Hello, sorry I am a big noob at this stuff, only recently started. But I’m essentially making a snake-dragon with a head, multiple body segments, and a tail (that I want to appear). The body pieces all follow the prefab that is in front of it(like a conga line), starting the instantiation at the head. I want this chain of pieces to end with the Tail prefab, but I’m not sure how to do it.
I’ve already tried doing something…but it doesn’t work(the script with SerpentTailMovement).
(Please let me know if I need to give more details. Also, lemme know if you can see the code attached, I’ve never asked a question on this website before)
Thanks! <3
the commented out code is what I tried (that isn’t working) in the serpenthead script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SerpentHead : MonoBehaviour
{
public SerpentSegmentMovement segmentPrefab;
public int numSegments = 3;
// public SerpentTailMovement tailPrefab;
//public int numTail = 1;
void Start()
{
//go through and generate numsegments
//lots of prefabs. Call the init method
//on each one, passing the previous gameobject
//as the leader.
GameObject leader = gameObject; //start with the head
for (int i = 0; i < numSegments; i++)
{
SerpentSegmentMovement s = Instantiate(segmentPrefab, transform.position, Quaternion.identity);
s.Init(leader);
leader = s.gameObject;
// SerpentTailMovement t = Instantiate(tailPrefab, transform.position, Quaternion.identity);
// t.Init(leader);
//leader = t.gameObject;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SerpentSegmentMovement : MonoBehaviour
{
public GameObject leader;
public float followSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (leader != null)
{
transform.position = Vector3.Lerp(transform.position, leader.transform.position, Time.deltaTime * followSpeed);
transform.LookAt(leader.transform.position, Vector3.up);
}
}
public void Init(GameObject previous)
{
leader = previous;
}
I duplicated some code in the serpent head script and gave the tail prefab the same script as the body segment prefab, and made its leader the body instead of the head. now I’m not sure what to do? explanations and hints are fine as well as I want to learn (but I also wouldn’t mind a bit o script)
image of my unity