Skip to content

Commit 4f1ea02

Browse files
committed
Added damped transform on segments
1 parent a356d12 commit 4f1ea02

13 files changed

+39387
-4216
lines changed

Assets/Scenes/SampleScene.unity

Lines changed: 39214 additions & 1760 deletions
Large diffs are not rendered by default.

Assets/Scripts/CentipedeController.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Animations.Rigging;
5+
6+
public class CentipedeController : MonoBehaviour
7+
{
8+
[SerializeField] private Transform headTarget;
9+
[SerializeField] private Transform head;
10+
11+
[SerializeField] private float moveSpeed;
12+
[SerializeField] private float turnAngle;
13+
14+
[SerializeField] private float frequency;
15+
[SerializeField] private float amplitude;
16+
17+
private float t = 0;
18+
private Rig rig;
19+
20+
private void Start()
21+
{
22+
rig = GetComponent<RigBuilder>().layers[0].rig;
23+
}
24+
25+
private void MoveHeadTarget()
26+
{
27+
t += Time.deltaTime;
28+
Vector3 pos = headTarget.position;
29+
pos.x += Mathf.Sin(t * Mathf.PI * frequency) * amplitude;
30+
headTarget.position = pos;
31+
}
32+
33+
// Update is called once per frame
34+
private void Update()
35+
{
36+
37+
38+
// Foward and backward movement
39+
if (Input.GetKey(KeyCode.W))
40+
{
41+
rig.weight = 1.0f;
42+
transform.position += transform.forward * moveSpeed;
43+
head.position = new Vector3(headTarget.position.x, head.position.y, head.position.z);
44+
// Turning
45+
if (Input.GetKey(KeyCode.Q))
46+
{
47+
transform.rotation *= Quaternion.Euler(0, -turnAngle, 0);
48+
49+
}
50+
else if (Input.GetKey(KeyCode.E))
51+
{
52+
transform.rotation *= Quaternion.Euler(0, turnAngle, 0);
53+
54+
}
55+
else
56+
{
57+
58+
MoveHeadTarget();
59+
}
60+
61+
}
62+
else if (Input.GetKey(KeyCode.S))
63+
{
64+
transform.position += -transform.forward * moveSpeed;
65+
}
66+
// Right and left movement
67+
else if (Input.GetKey(KeyCode.D))
68+
{
69+
transform.position += transform.right * moveSpeed;
70+
}
71+
else if (Input.GetKey(KeyCode.A))
72+
{
73+
transform.position += -transform.right * moveSpeed;
74+
}
75+
76+
if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.Q) || Input.GetKeyUp(KeyCode.E) )
77+
{
78+
rig.weight = 0.0f;
79+
headTarget.position = Vector3.zero;
80+
}
81+
}
82+
}

Assets/Scripts/MovementController.cs.meta renamed to Assets/Scripts/CentipedeController.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/LegMovement.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
public class LegMovement : MonoBehaviour
55
{
66
public Transform homeTransform; // The position and rotation we want to stay in range of
7-
public bool allowMovement; // Controlled by SegmentController
87
public bool Moving; // Is the leg moving?
98

109
[SerializeField] private float allowedDistanceFromHome; // Stay within this distance of home
1110
[SerializeField] private float moveOffset; // Adds an offset when moving the target towards home (Adds this value on local Z forward)
11+
[SerializeField] private float legBendHeightOffset; // How high will the leg bend up when moving
1212
[SerializeField] private float moveDuration; // How long a step takes to complete
1313
[SerializeField] private float dist;
1414

1515
private Vector3 lastPos;
1616

1717
private void Start()
1818
{
19-
allowMovement = true;
2019
lastPos = transform.position;
2120
}
2221

@@ -28,13 +27,8 @@ private IEnumerator MoveToHome()
2827

2928
while (timeElapsed < moveDuration)
3029
{
31-
while (!allowMovement)
32-
{
33-
yield return null;
34-
}
3530
timeElapsed += Time.deltaTime;
3631
float normalizedTime = timeElapsed / moveDuration;
37-
//transform.position = Vector3.Lerp(lastPos, endPoint, normalizedTime); // Interpolate position and rotation
3832
transform.position = Bezier(lastPos, endPoint, normalizedTime); // Interpolate position and rotation
3933

4034
yield return null;
@@ -44,42 +38,34 @@ private IEnumerator MoveToHome()
4438
Moving = false;
4539
}
4640

47-
private void Update()
41+
public void Move()
4842
{
49-
if (Moving) // If we are already moving, don't start another move
43+
if (Moving)
5044
{
5145
return;
5246
}
47+
48+
dist = Vector3.Distance(transform.position, homeTransform.position);
49+
50+
if (dist > allowedDistanceFromHome) // If we are too far off in position or rotation
51+
{
52+
StartCoroutine(MoveToHome()); // Start the step coroutine
53+
}
5354
else
5455
{
55-
dist = Vector3.Distance(transform.position, homeTransform.position);
56-
57-
if (dist > allowedDistanceFromHome) // If we are too far off in position or rotation
58-
{
59-
StartCoroutine(MoveToHome()); // Start the step coroutine
60-
}
61-
else
62-
{
63-
transform.position = lastPos;
64-
}
56+
transform.position = lastPos;
6557
}
58+
6659
}
6760

6861
Vector3 Bezier(Vector3 current, Vector3 target, float t)
6962
{
7063
Vector3 p0 = current;
71-
Vector3 p1 = (current + (target - current) / 2) + (Vector3.up * 2.0f);
64+
Vector3 p1 = (current + (target - current) / 2) + (Vector3.up * legBendHeightOffset);
7265
Vector3 p2 = target;
7366
Vector3 q1 = Vector3.Lerp(p0, p1, t);
7467
Vector3 q2 = Vector3.Lerp(p1, p2, t);
7568

7669
return Vector3.Lerp(q1, q2, t);
7770
}
78-
79-
void OnDrawGizmosSelected()
80-
{
81-
// Draw a yellow sphere at the transform's position
82-
Gizmos.color = Color.yellow;
83-
Gizmos.DrawSphere((lastPos + (homeTransform.position - lastPos)/2) + ((Vector3.up * 2.0f)), 1);
84-
}
8571
}

Assets/Scripts/LegPairController.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
4+
public class LegPairController : MonoBehaviour
5+
{
6+
[SerializeField] private int pairIndex;
7+
[SerializeField] private LegPairController pairInFront; // The pair of legs in front
8+
[Header("Legs")]
9+
[SerializeField] private LegMovement rightLeg;
10+
[SerializeField] private LegMovement leftLeg;
11+
12+
private SegmentController segmentController;
13+
14+
private void Start()
15+
{
16+
segmentController = transform.parent.GetComponent<SegmentController>();
17+
pairInFront = transform.parent.GetComponent<LegPairController>();
18+
StartCoroutine(MoveLegs());
19+
}
20+
21+
private IEnumerator MoveLegs()
22+
{
23+
while(true)
24+
{
25+
do
26+
{
27+
rightLeg.Move();
28+
if (pairInFront != null) { pairInFront.leftLeg.Move(); }
29+
yield return null;
30+
}
31+
while ((pairInFront != null) ? (pairInFront.leftLeg.Moving && rightLeg.Moving) : rightLeg.Moving);
32+
33+
do
34+
{
35+
leftLeg.Move();
36+
if (pairInFront != null) { pairInFront.rightLeg.Move(); }
37+
yield return null;
38+
}
39+
while ((pairInFront != null) ? (pairInFront.rightLeg.Moving && leftLeg.Moving) : leftLeg.Moving);
40+
}
41+
}
42+
43+
//public LegPairController SetIndex(int index)
44+
//{
45+
// pairIndex = index;
46+
// pairInFront = (pairIndex == 0) ? null : segmentController.segments[pairIndex - 1];
47+
// return this;
48+
//}
49+
}

Assets/Scripts/LegPairController.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/MovementController.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Assets/Scripts/SegmentController.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
13
using UnityEngine;
24

35
public class SegmentController : MonoBehaviour
46
{
5-
[Header("Right Leg")]
6-
[SerializeField] private LegMovement rightLeg;
7-
[Header("Left Leg")]
8-
[SerializeField] private LegMovement leftLeg;
7+
public List<LegPairController> segments = new List<LegPairController>();
98

10-
11-
// Update is called once per frame
12-
void Update()
9+
private void Start()
1310
{
14-
if (rightLeg.Moving)
15-
{
16-
rightLeg.allowMovement = true;
17-
leftLeg.allowMovement = false;
18-
}
19-
else if(leftLeg.Moving)
20-
{
21-
leftLeg.allowMovement = true;
22-
rightLeg.allowMovement = false;
23-
}
11+
//for (int i = 0; i < transform.childCount; i++)
12+
//{
13+
// segments.Add(transform.GetChild(i).GetComponent<LegPairController>().SetIndex(i));
14+
//}
15+
16+
//for (int i = 0; i < transform.childCount; i++)
17+
//{
18+
// segments.Add(transform.GetChild(i).GetComponent<LegPairController>().SetIndex(i));
19+
//}
2420
}
2521
}

Assets/Scripts/SegmentController.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)