hi. guys. i’m making a sort of 3d snake game by unity scripts and someone’s guide source.
but i can’t make a snake tail chain and a classical snake’s tail movement.
i want to make a my snake game like a this youtube video.
addedly, i had ever tried the HingeJoint and SmoothFollow Component. but They are not what I wanted.
please help me.
please check my code.
private var desiredBearing: String = 'South';
private var currentBearing: String;
var moveSpeed : float = 1.5;
var gridSize : int = 1;
enum Orientation {Horizontal, Vertical}
var gridOrientation = Orientation.Horizontal;
var allowDiagonals = false;
var correctDiagonalSpeed = true;
private var input = Vector2(0,-1);
var myTransform: Transform;
var TailPrefab : GameObject;
var ApplePrefab : GameObject;
private var counter = 0;
private var lastChain : GameObject = null;
function Awake()
{
myTransform = transform;
}
function Start ()
{
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
var myTransform = transform;
var startPosition : Vector3;
var endPosition : Vector3;
var t : float;
var tx : float;
while (true)
{
while (input == Vector2.zero)
{
GetAxes();
tx = 0.0;
yield;
}
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
startPosition = myTransform.position;
endPosition = gridOrientation == Orientation.Horizontal?
Vector3(Mathf.Round(myTransform.position.x), 0.0, Mathf.Round(myTransform.position.z)) +
Vector3(System.Math.Sign(input.x)*gridSize, 0.0, System.Math.Sign(input.y)*gridSize)
:
Vector3(Mathf.Round(myTransform.position.x), Mathf.Round(myTransform.position.y), 0.0) +
Vector3(System.Math.Sign(input.x)*gridSize, System.Math.Sign(input.y)*gridSize, 0.0);
t = tx;
while (t < 1.0)
{
myTransform.position = Vector3.Lerp(startPosition, endPosition, t);
yield;
t += Time.deltaTime * (moveSpeed/gridSize) * (correctDiagonalSpeed input.x != 0.0 input.y != 0.0? .7071 : 1.0);
}
tx = t - 1.0;
GetAxes();
}
}
function Update ()
{
var horizontalMovement = Input.GetAxis("Horizontal");
var verticalMovement = Input.GetAxis("Vertical");
if (horizontalMovement > 0 currentBearing != 'West')
{
desiredBearing = 'East';
}
else if (horizontalMovement < 0 currentBearing != 'East')
{
desiredBearing = 'West';
}
else if (verticalMovement > 0 currentBearing != 'South')
{
desiredBearing = 'North';
}
else if (verticalMovement < 0 currentBearing != 'North')
{
desiredBearing = 'South';
}
var r = Random.Range(0, 100);
if(r > 90 GameObject.FindWithTag("apples") == null)
{
var range = 3;
Instantiate(ApplePrefab, Vector3(Random.Range(-range,range), 1, Random.Range(-range,range)), Quaternion(0,0,0,1));
}
}
function GetAxes ()
{
input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (desiredBearing == 'East')
{
currentBearing = desiredBearing;
input.x = 1;
input.y = 0;
}
else if (desiredBearing == 'West')
{
currentBearing = desiredBearing;
input.x = -1;
input.y = 0;
}
else if (desiredBearing == 'North')
{
currentBearing = desiredBearing;
input.y = 1;
input.x = 0;
}
else if (desiredBearing == 'South')
{
currentBearing = desiredBearing;
input.y = -1;
input.x = 0;
}
}
function OnCollisionEnter (c : Collision)
{
if (c.gameObject == null)
return;
if (c.gameObject.CompareTag("Respawn"))
{
Application.LoadLevel("test1");
}
if (c.rigidbody == null)
return;
if (c.gameObject.name == "Apple(Clone)")
{
var chew : GameObject = null;
if(Random.value > 0.5)
{
chew = GameObject.Find("GetSound1");
}
else
{
chew = GameObject.Find("GetSound2");
}
chew.audio.Play();
Destroy(c.gameObject);
snake_addTail();
return;
}
}
function snake_addTail()
{
if(lastChain == null)
{
lastChain = gameObject;
}
var newChain : GameObject = Instantiate(TailPrefab, lastChain.transform.position - lastChain.transform.forward * 0.5, Quaternion(0,0,0,0));
newChain.transform.rotation = lastChain.transform.rotation;
newChain.name = "Tail " + counter;
counter++;
}