Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by nurinafeeeqah · Sep 16, 2017 at 04:41 AM · snakefollow pathlong

How to make the snake grow in size using C# in Unity in 3D?

I have an assignment that asked to make a retro game using Unity. My snake head can move around but cannot grow in size when collecting item. Help?

This is my script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class control: MonoBehaviour {

 public List<Transform> BodyParts = new List<Transform>();

 public float mindistance = 0.25f;

 public int beginsize;

 public float speed = 1;
 public float rotationspeed = 50;

 public float TimeFromLastRetry;

 public GameObject bodyprefab;

 private float dis;
 private Transform curBodyPart;
 private Transform PrevBodypart;

 public bool IsAlive;

 private Rigidbody rb;
 private GameManager gm;

 // Use this for initialization
 void Start () {

     rb = GetComponent<Rigidbody> ();

     GameObject thescore = GameObject.Find ("Game Manager");
     gm = thescore.GetComponent<GameManager> ();

 }

 // Update is called once per frame
 void Update () {

     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVertical = Input.GetAxis ("Vertical");

     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
     //get direction from the values

     rb.AddForce (movement * speed);
     //addForce = how strong the ball is pushed

 }

 void OnTriggerEnter(Collider other){
     if(other.gameObject.CompareTag ("Pick Up")){
         other.gameObject.SetActive(false);

         gm.score = gm.score + 1;
         gm.SetScore ();
     }
 }

 public void Move()
 {
     float curspeed = speed; //snake always move

     if (Input.GetKey(KeyCode.W))
         curspeed *=2;

     BodyParts [0].Translate (BodyParts [0].forward * curspeed * Time.smoothDeltaTime, Space.World);

     if (Input.GetAxis ("Horizontal") != 0)
         BodyParts [0].Rotate (Vector3.up * rotationspeed * Time.deltaTime * Input.GetAxis ("Horizontal"));

     for (int i = 1; i < BodyParts.Count; i++ )
     {
         curBodyPart = BodyParts [i];
         PrevBodypart = BodyParts [i - 1];

         dis = Vector3.Distance (PrevBodypart.position, curBodyPart.position);

         Vector3 newpos = PrevBodypart.position;

         newpos.y = BodyParts[0].position.y;

         float T = Time.deltaTime * dis / mindistance * curspeed; 

         if (T > 0.5f)
             T = 0.5f;
         curBodyPart.position = Vector3.Slerp (curBodyPart.position, newpos, T);
         curBodyPart.rotation = Quaternion.Slerp (curBodyPart.rotation, PrevBodypart.rotation, T);

     }

 }



 public void AddBodyPart()
 { 
     Transform newpart = (Instantiate (bodyprefab, BodyParts [BodyParts.Count - 1].position, BodyParts [BodyParts.Count - 1].rotation) as GameObject).transform;

     newpart.SetParent (transform);
     BodyParts.Add (newpart);

 }
     

}

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 Positive7 · Sep 16, 2017 at 05:07 AM

You never call AddBodyPart() maybe add it to OnTrigger function.

  void OnTriggerEnter(Collider other){
          if(other.gameObject.CompareTag ("Pick Up")){
              other.gameObject.SetActive(false);
              gm.score = gm.score + 1;
              gm.SetScore ();
              AddBodyPart();
          }
      }


EDIT : Also you might want to match the velocity as well :

 private void AddBodyPart()
     {
         var newpart = Instantiate(
                                   bodyprefab,
                                   BodyParts[BodyParts.Count - 1].position,
                                   BodyParts[BodyParts.Count - 1].rotation).transform;
         newpart.position = new Vector3(
                                        BodyParts[BodyParts.Count - 1].position.x,
                                        BodyParts[BodyParts.Count - 1].position.y,
                                        BodyParts[BodyParts.Count - 1].position.z - 1);

         newpart.GetComponent<Rigidbody>().velocity = BodyParts[BodyParts.Count - 1]
             .gameObject.GetComponent<Rigidbody>().velocity;
         newpart.SetParent(transform);
         BodyParts.Add(newpart);
     }
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

69 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

Related Questions

Make an object following another object 0 Answers

How to work with long type in inspector or propertydrawer 0 Answers

Is this an [optimal, adequate, ftarded] way to make a snake follow its head? 2 Answers

2 Player Snake Game Not Working 0 Answers

how to follow curved path? 4 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