Skip to content

Commit 4de0ae6

Browse files
committed
Adds FloatPhysics (coroutine version)
Hold my beer while I see if I can now remove the 78% CPU (15 FPS!) performance issue it added
1 parent 6086978 commit 4de0ae6

File tree

8 files changed

+256
-33
lines changed

8 files changed

+256
-33
lines changed

Assets/_Project/Scripts/Physics.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEngine;
2+
3+
public class ColliderBounds : MonoBehaviour
4+
{
5+
Collider m_Collider;
6+
Vector3 m_Center;
7+
Vector3 m_Size, m_Min, m_Max;
8+
9+
void Start()
10+
{
11+
//Fetch the Collider from the GameObject
12+
m_Collider = GetComponent<Collider>();
13+
//Fetch the center of the Collider volume
14+
m_Center = m_Collider.bounds.center;
15+
//Fetch the size of the Collider volume
16+
m_Size = m_Collider.bounds.size;
17+
//Fetch the minimum and maximum bounds of the Collider volume
18+
m_Min = m_Collider.bounds.min;
19+
m_Max = m_Collider.bounds.max;
20+
//Output this data into the console
21+
OutputData();
22+
}
23+
24+
void OutputData()
25+
{
26+
//Output to the console the center and size of the Collider volume
27+
Debug.Log("Collider Center : " + m_Center);
28+
Debug.Log("Collider Size : " + m_Size);
29+
Debug.Log("Collider bound Minimum : " + m_Min);
30+
Debug.Log("Collider bound Maximum : " + m_Max);
31+
}
32+
}

Assets/_Project/Scripts/Physics/ColliderBounds.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.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class FloatPhysics : MonoBehaviour
6+
{
7+
// container
8+
public Collider worldContainerCollider;
9+
10+
11+
Rigidbody rb; // rb to apply force
12+
public float forcemultiplier; // use to generate new force directions
13+
public bool applyNewForce; // should we apply new force on loop?
14+
public float newForceTime; // the time before the current force expires
15+
public Vector3 force; // the force vector (speed + direction)
16+
public Vector3 forceInfluence; // influence direction of force vector if it gets close to the boundaries
17+
public float maxSpeed = 30f; // max speed of rb
18+
19+
public Vector3 mousePos;
20+
21+
void Awake()
22+
{
23+
worldContainerCollider = GameObject.Find("WorldContainer").GetComponent<Collider>();
24+
rb = GetComponent<Rigidbody>();
25+
}
26+
27+
private void Update()
28+
{
29+
// first run
30+
applyNewForce = true;
31+
// 1. start the force loop
32+
StartCoroutine(ForceVectorGenerator());
33+
}
34+
35+
void FixedUpdate()
36+
{
37+
if (rb.velocity.magnitude > maxSpeed)
38+
{
39+
rb.velocity = rb.velocity.normalized * maxSpeed;
40+
}
41+
if (applyNewForce)
42+
{
43+
// add whatever force is stored in the vector
44+
rb.AddForce(force + forceInfluence);
45+
applyNewForce = false;
46+
}
47+
}
48+
49+
50+
// generate a new random direction at random times
51+
IEnumerator ForceVectorGenerator()
52+
{
53+
// run forever
54+
while (true)
55+
{
56+
// pick new time to wait before generating new force vector
57+
newForceTime = Random.Range(4.0f, 8.0f);
58+
59+
// if the GameObject has left the scene then push it back
60+
forceInfluence = ForceAwayFromWall(worldContainerCollider.bounds);
61+
62+
// generate new force vector
63+
force = new Vector3(
64+
Random.Range(-forcemultiplier, forcemultiplier),
65+
Random.Range(-forcemultiplier, forcemultiplier),
66+
Random.Range(-forcemultiplier, forcemultiplier)
67+
);
68+
69+
applyNewForce = true;
70+
71+
// wait for before next loop
72+
yield return new WaitForSeconds(newForceTime);
73+
}
74+
75+
}
76+
77+
78+
Vector3 ForceAwayFromWall(Bounds bounds)
79+
{
80+
Vector3 newForce = new Vector3(0, 0, 0);
81+
82+
// X
83+
if (gameObject.transform.position.x < bounds.min.x)
84+
{
85+
newForce.x += 2;
86+
}
87+
else if (gameObject.transform.position.x > bounds.max.x)
88+
{
89+
newForce.x -= 2;
90+
}
91+
92+
// Y
93+
if (gameObject.transform.position.y < bounds.min.y)
94+
{
95+
newForce.y += 2;
96+
}
97+
else if (gameObject.transform.position.y > bounds.max.y)
98+
{
99+
newForce.y -= 2;
100+
}
101+
102+
// Z
103+
if (gameObject.transform.position.z < bounds.min.z)
104+
{
105+
newForce.z += 2;
106+
}
107+
else if (gameObject.transform.position.z > bounds.max.z)
108+
{
109+
newForce.z -= 2;
110+
}
111+
return newForce;
112+
}
113+
114+
115+
116+
}
File renamed without changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
6+
// found online https://answers.unity.com/questions/59934/how-to-an-object-floating-up-and-down.html
7+
// manipulates transform :-(
8+
9+
10+
public class FloatTransform : MonoBehaviour
11+
{
12+
13+
public bool animPos = true;
14+
public Vector3 posAmplitude = Vector3.one;
15+
public Vector3 posSpeed = Vector3.one;
16+
17+
public bool animRot = true;
18+
public Vector3 rotAmplitude = Vector3.one * 20;
19+
public Vector3 rotSpeed = Vector3.one;
20+
21+
public bool animScale = false;
22+
public Vector3 scaleAmplitude = Vector3.one * 0.1f;
23+
public Vector3 scaleSpeed = Vector3.one;
24+
25+
private Vector3 origPos;
26+
private Vector3 origRot;
27+
private Vector3 origScale;
28+
29+
private float startAnimOffset = 0;
30+
31+
32+
/**
33+
* Awake
34+
*/
35+
void Awake()
36+
{
37+
origPos = transform.position;
38+
origRot = transform.eulerAngles;
39+
origScale = transform.localScale;
40+
startAnimOffset = Random.Range(0f, 540f); // so that the xyz anims are already offset from each other since the start
41+
}
42+
43+
/**
44+
* Update
45+
*/
46+
void Update()
47+
{
48+
/* position */
49+
if (animPos)
50+
{
51+
Vector3 pos;
52+
pos.x = origPos.x + posAmplitude.x * Mathf.Sin(posSpeed.x * Time.time + startAnimOffset);
53+
pos.y = origPos.y + posAmplitude.y * Mathf.Sin(posSpeed.y * Time.time + startAnimOffset);
54+
pos.z = origPos.z + posAmplitude.z * Mathf.Sin(posSpeed.z * Time.time + startAnimOffset);
55+
transform.position = pos;
56+
}
57+
58+
/* rotation */
59+
if (animRot)
60+
{
61+
Vector3 rot;
62+
rot.x = origRot.x + rotAmplitude.x * Mathf.Sin(rotSpeed.x * Time.time + startAnimOffset);
63+
rot.y = origRot.y + rotAmplitude.y * Mathf.Sin(rotSpeed.y * Time.time + startAnimOffset);
64+
rot.z = origRot.z + rotAmplitude.z * Mathf.Sin(rotSpeed.z * Time.time + startAnimOffset);
65+
transform.eulerAngles = rot;
66+
}
67+
68+
/* scale */
69+
if (animScale)
70+
{
71+
Vector3 scale;
72+
scale.x = origScale.x * (1 + scaleAmplitude.x * Mathf.Sin(scaleSpeed.x * Time.time + startAnimOffset));
73+
scale.y = origScale.y * (1 + scaleAmplitude.y * Mathf.Sin(scaleSpeed.y * Time.time + startAnimOffset));
74+
scale.z = origScale.z * (1 + scaleAmplitude.z * Mathf.Sin(scaleSpeed.z * Time.time + startAnimOffset));
75+
transform.localScale = scale;
76+
}
77+
}
78+
}

Assets/_Project/Scripts/Physics/FloatTransform.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/_Project/Scripts/Player/Physics_Float.cs

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

0 commit comments

Comments
 (0)