- Home /
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
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);
}