summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs51
1 files changed, 51 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs
new file mode 100644
index 0000000..af27989
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/Scenes/OldExamples/Example18_RTS/RTSTiltInMovementDirection.cs
@@ -0,0 +1,51 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Pathfinding;
+
+namespace Pathfinding.Examples {
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/rtstiltinmovementdirection.html")]
+ public class RTSTiltInMovementDirection : MonoBehaviour {
+ public Transform target;
+ public float amount;
+ public float speed;
+ public AudioSource motorSound;
+ public float soundGain = 1f;
+ public float soundPitchGain = 1f;
+ public float soundIdleVolume = 0.5f;
+ public float soundAdjustmentSpeed = 2;
+
+ [Range(0, 1)]
+ public float accelerationFraction = 0.5f;
+ IAstarAI ai;
+ Vector3 lastVelocity;
+
+ Vector3 smoothAcceleration;
+
+ // Use this for initialization
+ void Awake () {
+ ai = GetComponent<IAstarAI>();
+ if (motorSound != null) {
+ motorSound.time = Random.value * motorSound.clip.length;
+ }
+ }
+
+ // Update is called once per frame
+ void LateUpdate () {
+ var acc = Vector3.Lerp(ai.velocity, (ai.velocity - lastVelocity)/Time.deltaTime, accelerationFraction);
+
+ lastVelocity = ai.velocity;
+
+ smoothAcceleration = Vector3.Lerp(smoothAcceleration, acc, Time.deltaTime * 10);
+
+ var dir = Vector3.up + smoothAcceleration * amount;
+ Debug.DrawRay(target.position, dir, Color.blue);
+ var targetRot = Quaternion.LookRotation(dir, -target.forward) * Quaternion.Euler(90, 0, 0);
+ target.rotation = Quaternion.Slerp(target.rotation, targetRot, Time.deltaTime * speed);
+ if (motorSound != null) {
+ motorSound.volume = Mathf.Lerp(motorSound.volume, Mathf.Log(smoothAcceleration.magnitude+1)*soundGain + soundIdleVolume, soundAdjustmentSpeed * Time.deltaTime);
+ motorSound.pitch = Mathf.Lerp(motorSound.pitch, Mathf.Log(smoothAcceleration.magnitude+1)*soundPitchGain + 1f, soundAdjustmentSpeed * Time.deltaTime);
+ }
+ }
+ }
+}