snake type game (dynamic array / list)
hi, I am trying to make a game like Snake but I'm having some problems with the main character. I have code that allows a set amount of pieces that have to be assigned in the inspector, however, what I want is for the player to start with 4 parts of a body and on collision for a new part to be generated and assigned a primitive.
the problem I am coming across is that I need the transform of the object to add to the list version but I don't know how to generate a new object then assign its transform to the list.
public Transform[] chains; // static from inspector
const float dist = 2;
List<Transform> transList = new List<Transform>();
void Update () {
ChainLink ();
//ChainLinkList ();
}
void ChainLink(){
Vector3 prevLocation = transform.position; //set the prevLocation to the currant Position
foreach (Transform chain in chains) {
Vector3 direction = (prevLocation - chain.position).normalized; //set direction to normalised version of position - the prevLocation
chain.position = prevLocation - direction * dist;
prevLocation = chain.position;
}
}
// void ChainLinkList(){
// Vector3 prevLocation = transform.position; //set the prevLocation to the currant Position
//
// foreach (Transform chainList in transList) {
// Vector3 direction = (prevLocation - chainList.position).normalized; //set direction to normalised version of position - the prevLocation
// chainList.position = prevLocation - direction * dist;
// prevLocation = chainList.position;
// }
// }
//
// void OnCollisionEnter(){
// transList.Add ( // need the transform of the object to add); // GameObject.CreatePrimitive (PrimitiveType.Sphere);
// }
any help would be appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122322im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to program two buttons to appear when the player in my game dies 0 Answers
Copying a list of lists of int's in c# 1 Answer
Remembering list of camera positions is not working 0 Answers
Merging multiple array's from a specific class without manually targeting them 0 Answers
Only spawning power ups that the player wants in that game 1 Answer