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 *;*
-
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);*
- }*
}