Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DevDeedSquire · Feb 22, 2018 at 12:21 AM · listsfollowing

Advanced Snake

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[i].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);
             }
         }
     }
 
     /// <summary>
     /// swap the x and y values to turn left regardless of current Direction
     /// </summary>
     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));
         }
     }
 
     /// <summary>
     /// swap the x and y values to turn left regardless of current Direction
     /// </summary>
     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;
 
     /// <summary>
     /// Assign the data for the waypoint
     /// </summary>
     /// <param name="pos">The currect position of the head when its direction is changed</param>
     /// <param name="dir">The new direction of the Head at this 'pos'</param>
     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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DevDeedSquire · Feb 22, 2018 at 12:25 AM

Just noticed this error in Unity with it

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Waypoint].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Fireball.Update () (at Assets/Scripts/Fireball.cs:96)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a grid and turn it to List 1 Answer

Coding Syntax question 2 Answers

items not adding to list 1 Answer

Fill Lists recursevely 0 Answers

Sorting a List {string, int} 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges