I’m making an advanced snake game where the snake is a fire trail, I’ve got a lot going on in the ‘Fireball’ class but it’s almost working as intended, I’ve set up lists for the segments to follow the Head based on location of the Head’s turn etc, it works fine with 5 segments, it gets a little complicated with 50 though and random segments seem to fly off in random directions.
Here’s the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fireball : MonoBehaviour {
//The Direction the object is moving in
public Vector2 direction = new Vector2(0,1);
//The speed of the object (tweak to change difficulty)
public static float speed = 5f;
//The Rigidbody of the object
public Rigidbody2D rigidBody;
[Header("AI Level")]
//Is this object leading other objects
public bool isHead = false;
//Is this object taking player input
public bool isPlayer = false;
[Header("Tail Object Properties")]
//List of waypoints updated by the head to allow the following AI
List<Waypoint> nextDirection = new List<Waypoint>();
[Header("Head Object Properties")]
//List of tail segments, used if this.isHead = true
List<Fireball> tailSegments = new List<Fireball>();
public GameObject tailPrefab;
[Header("Player Object Properties")]
//The intial tail segments that would be created if the this.isHead = true;
public int startingTailSegments = 0;
[Header("AI Properties")]
//The random direction behaviour
public float turnDelay = 1f;
public float timeSinceTurn = 0f;
private void Start()
{
//Get this GameObjects Rigidbody2D
rigidBody = GetComponent<Rigidbody2D>();
//Assign the starting segments into the heads list
if (isHead)
{
for (int i = 0; i < startingTailSegments; i++)
{
GameObject t = Instantiate(tailPrefab, new Vector3(transform.position.x - (i + 1), transform.position.y), Quaternion.identity);
tailSegments.Add(t.GetComponent<Fireball>());
tailSegments*.direction = this.direction;*
}
}
}
private void FixedUpdate()
{
//move the GO in the direction
rigidBody.velocity = direction * speed;
//Set the GO to face the direction it is moving
transform.up = rigidBody.velocity;
}
private void Update()
{
//If this GO is a head element
if (isHead)
{
//Is this GO taking player input
if (isPlayer)
{
if (Input.GetKeyDown(KeyCode.A))
{
TurnLeft();
}
if (Input.GetKeyDown(KeyCode.D))
{
TurnRight();
}
}
else
{
//Randomly turn
timeSinceTurn += Time.deltaTime;
if (timeSinceTurn >= turnDelay)
{
timeSinceTurn = 0;
if (Random.Range(-10, 10) >= 0)
TurnLeft();
else
TurnRight();
}
}
}else
{
//Check location against next direction list and turn if time to
Vector2 pos2D = new Vector2(transform.position.x, transform.position.y);
if (pos2D == nextDirection[0].position)
{
direction = nextDirection[0].newDir;
//Remove this turn from the list once completed
nextDirection.RemoveAt(0);
}
}
}
///
/// swap the x and y values to turn left regardless of current Direction
///
public void TurnLeft()
{
Vector2 newDir = new Vector2(direction.y * -1, direction.x * 1);
direction = newDir;
//Add the turn to the segments nextDirection lists
foreach (Fireball tail in tailSegments)
{
tail.nextDirection.Add(new Waypoint(new Vector2(transform.position.x, transform.position.y), this.direction));
}
}
///
/// swap the x and y values to turn left regardless of current Direction
///
public void TurnRight()
{
Vector2 newDir = new Vector2(direction.y * 1, direction.x * -1);
direction = newDir;
//Add the turn to the segments nextDirection lists
foreach (Fireball tail in tailSegments)
{
tail.nextDirection.Add(new Waypoint(new Vector2(transform.position.x, transform.position.y), this.direction));
}
}
}
[System.Serializable]
public class Waypoint : MonoBehaviour
{
//The position the object changes direction
public Vector2 position;
//The new direction for the object when it reaches ‘position’
public Vector2 newDir;
///
/// Assign the data for the waypoint
///
/// The currect position of the head when its direction is changed
/// The new direction of the Head at this ‘pos’
public Waypoint(Vector2 pos, Vector2 dir)
{
this.position = pos;
this.newDir = dir;
}
}
no doubt i’m missing something obvious :), I think something is getting confused and the tail segments seem to be getting the wrong direction in the lists randomly
any help would be appreciated
