help with a unity/c# basic game bug

So, whilst im on my summer hols i have been programming small games, basically just to keep stuff in my head, and in one of my games, a snake like, i have a bug where the parts are supposed to be 0.125f apart, and they all are apart from the distance between the second and first parts. I have been trying to solve this for weeks, and am probably just a bit code-blind so im asking for help.

http://imgur.com/IEqAvhk

what i beleive to be the relevant code;
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;
        }
    }


P.S the movement of the block is probably badly coded because i am fairly new, any general tips and cleanup is generally welcome also. the blocks are moving around a sphere hence the perhaps odd seeming movement code.
Last edited on
Topic archived. No new replies allowed.