瀏覽代碼

根据平台角度转动角色

WGL 4 月之前
父節點
當前提交
bfa49b79b7

+ 23 - 2
ActionTowerDefense/Assets/Scripts/Characters/Demonic.cs

@@ -313,7 +313,18 @@ public class Demonic : MoveCharacter
                     velocity.y = 0;
                     if (!foot.haveGravity)
                     {
-                        transform.position = new Vector3(transform.position.x, platformY, transform.position.z);
+                        transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
+                        Quaternion targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
+                        if (RotLerpTime < 1)
+                        {
+                            RotLerpTime += RotLerpSpeed * Time.deltaTime;
+                            transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
+                        }
+                        else
+                        {
+                            transform.rotation = targetQt;
+                        }
+
                     }
                     rb.velocity = velocity;
 
@@ -371,7 +382,17 @@ public class Demonic : MoveCharacter
                     velocity.y = 0;
                     if (!foot.haveGravity)
                     {
-                        transform.position = new Vector3(transform.position.x, platformY, transform.position.z);
+                        transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
+                        Quaternion targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
+                        if (RotLerpTime < 1)
+                        {
+                            RotLerpTime += RotLerpSpeed * Time.deltaTime;
+                            transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
+                        }
+                        else
+                        {
+                            transform.rotation = targetQt;
+                        }
                     }
                         
                     rb.velocity = velocity;

+ 22 - 2
ActionTowerDefense/Assets/Scripts/Characters/Enemy.cs

@@ -253,7 +253,17 @@ public class Enemy : MoveCharacter
                     velocity.y = 0;
                     if (!foot.haveGravity)
                     {
-                        transform.position = new Vector3(transform.position.x, platformY, transform.position.z);
+                        transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
+                        Quaternion targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
+                        if (RotLerpTime < 1)
+                        {
+                            RotLerpTime += RotLerpSpeed * Time.deltaTime;
+                            transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
+                        }
+                        else
+                        {
+                            transform.rotation = targetQt;
+                        }
                     }
                     rb.velocity = velocity;
                 }
@@ -309,7 +319,17 @@ public class Enemy : MoveCharacter
                     velocity.y = 0;
                     if (!foot.haveGravity)
                     {
-                        transform.position = new Vector3(transform.position.x, platformY, transform.position.z);
+                        transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
+                        Quaternion targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
+                        if (RotLerpTime < 1)
+                        {
+                            RotLerpTime += RotLerpSpeed * Time.deltaTime;
+                            transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
+                        }
+                        else
+                        {
+                            transform.rotation = targetQt;
+                        }
                     }
                         
                     rb.velocity = velocity;

+ 21 - 5
ActionTowerDefense/Assets/Scripts/Characters/Foot.cs

@@ -30,12 +30,13 @@ public class Foot : MonoBehaviour
     {
         if (trigGroundList.Count == 0)
         {
-            if (!haveGravity|| moveCharacter.platformY != 0)
+            if (!haveGravity|| moveCharacter.platformPosY != 0 || moveCharacter.platformRotZ!=0)
             {
                 rb.useGravity = true;
                 haveGravity = true;
                 moveCharacter.velocityAddition = 0;
-                moveCharacter.platformY = 0;
+                moveCharacter.platformPosY = 0;
+                moveCharacter.platformRotZ = 0;
             }
         }
         else
@@ -67,8 +68,17 @@ public class Foot : MonoBehaviour
                     {
                         rb.useGravity = false;
                         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
-                        moveCharacter.platformY = platform.GetPosY(rb.transform.position);
-                        rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformY, rb.transform.position.z);
+                        moveCharacter.platformPosY = platform.GetPosY(rb.transform.position);
+                        float targetRotZ = platform.GetRotZ();
+                        if (targetRotZ != moveCharacter.platformRotZ)
+                        {
+                            moveCharacter.platformRotZ = targetRotZ;
+                            moveCharacter.RotLerpTime = 0;
+                        }
+                        
+                        rb.transform.position = new Vector3(rb.transform.position.x, moveCharacter.platformPosY, rb.transform.position.z);
+                        
+                        //rb.transform.rotation = Quaternion.Euler(0, 0, moveCharacter.platformRotZ);
                         haveGravity = false;
 
                     }
@@ -79,7 +89,13 @@ public class Foot : MonoBehaviour
                     {
                         moveCharacter.velocityAddition = platform.rb.velocity.x;
                     }
-                    moveCharacter.platformY = platform.GetPosY(rb.transform.position);
+                    moveCharacter.platformPosY = platform.GetPosY(rb.transform.position);
+                    float targetRotZ = platform.GetRotZ();
+                    if (targetRotZ != moveCharacter.platformRotZ)
+                    {
+                        moveCharacter.platformRotZ = targetRotZ;
+                        moveCharacter.RotLerpTime = 0;
+                    }
 
                 }
             }

+ 4 - 1
ActionTowerDefense/Assets/Scripts/Characters/MoveCharacter.cs

@@ -23,7 +23,10 @@ public class MoveCharacter : Character
     [FoldoutGroup("塗俋笭薯")] [LabelText("輪華")] public float extraGroundGravity = -28.8f;
 
     [FoldoutGroup("ⅸ怢", order: -1)] [LabelText("痄厒崝講")]public float velocityAddition;
-    [FoldoutGroup("ⅸ怢")] [DisplayOnly] public float platformY;
+    [FoldoutGroup("ⅸ怢")] [DisplayOnly] public float platformPosY;
+    [FoldoutGroup("ⅸ怢")] [DisplayOnly] public float platformRotZ;
+    [FoldoutGroup("ⅸ怢")] public float RotLerpSpeed = 0.6f;
+    [FoldoutGroup("ⅸ怢")] [DisplayOnly] public float RotLerpTime;
 
     [Header("第窐")]
     public float matState = 1;

+ 11 - 5
ActionTowerDefense/Assets/Scripts/Platform.cs

@@ -62,15 +62,21 @@ public class Platform : MonoBehaviour
         }
     }
 
-    
+    //获取角色站在平台上的Y轴位置
     public float GetPosY(Vector3 target)
     {
         float angleInRadians = transform.eulerAngles.z * Mathf.Deg2Rad;
         float tanTheta = Mathf.Tan(angleInRadians);
-        Vector3 localPos = transform.parent.transform.InverseTransformPoint(target);
-        localPos.y = localPos.x * tanTheta;
-        Vector3 worldPos = transform.parent.transform.TransformPoint(localPos);
-        return worldPos.y;
+        float disX = target.x - transform.parent.position.x;
+        float disY = disX * tanTheta;
+        float targetY = disY + transform.parent.position.y;
+        return targetY;
 
     }
+
+    //获取平台Z轴旋转
+    public float GetRotZ()
+    {
+        return transform.rotation.eulerAngles.z;
+    }
 }

+ 5 - 1
ActionTowerDefense/Assets/Scripts/SceneInteraction/SceneTransmit.cs

@@ -12,7 +12,11 @@ public class SceneTransmit : MonoBehaviour
         {
             foreach(var item in playerController.foot.trigGroundList)
             {
-                item.GetComponent<Platform>().feet.Remove(playerController.foot);
+                Platform platform = item.GetComponent<Platform>();
+                if (platform != null)
+                {
+                    platform.feet.Remove(playerController.foot);
+                }
             }
             playerController.foot.trigGroundList.Clear();
             playerController.transform.position = transmitTarget.position;