1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
void Update()
{
foreach (GameObject part in snakeBodyPartsList)
{
moveSpeed = 30 + (snakeBodyPartsList.Count * 2.5f);
if (part == gameObject)
{
if (Input.GetKey("a") || Input.GetKey("d"))
{
var turnRate = Input.GetKey("a") ? -120 : 120;
part.transform.parent.Rotate(0, 0, turnRate * Time.deltaTime);
}
part.transform.rotation = Quaternion.LookRotation(transform.forward, part.transform.position - sphere.transform.position);
part.transform.parent.rotation = Quaternion.AngleAxis(moveSpeed * Time.deltaTime, Vector3.Cross(part.transform.parent.position - part.transform.position, part.transform.forward)) * part.transform.parent.rotation;
}
else
{
part.transform.rotation = Quaternion.LookRotation(prevPart.transform.position, part.transform.position - sphere.transform.position);
if (prevPart != null && Vector3.Distance(part.transform.position, prevPart.position) > 0.125f)
{
part.transform.parent.rotation = Quaternion.AngleAxis(moveSpeed * Time.deltaTime, Vector3.Cross(part.transform.parent.position - part.transform.position, -(prevPart.position - part.transform.position).normalized * 0.125f)) * part.transform.parent.rotation;
}
}
if (Vector3.Distance(part.transform.position, sphere.transform.position) > 1.05)
{
while (Vector3.Distance(part.transform.position, sphere.transform.position) >= 1.045)
{
part.transform.position = new Vector3(part.transform.position.x, part.transform.position.y, part.transform.position.z - 0.001f);
}
}
else if (Vector3.Distance(part.transform.position, sphere.transform.position) < 1.04)
{
while (Vector3.Distance(part.transform.position, sphere.transform.position) <= 1.045)
{
part.transform.position = new Vector3(part.transform.position.x, part.transform.position.y, part.transform.position.z + 0.001f);
}
}
prevPart = part.transform;
}
}
|