diff options
author | chai <215380520@qq.com> | 2023-10-18 10:16:32 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-18 10:16:32 +0800 |
commit | 4ccd4bc6d126e0e0f51a50aa10c85b9bf48b1210 (patch) | |
tree | 9ac931da935b59a8d7c57ff0b6d90b88a0e5a479 /ActiveRagdoll/Assets/Scripts |
+ init
Diffstat (limited to 'ActiveRagdoll/Assets/Scripts')
23 files changed, 789 insertions, 0 deletions
diff --git a/ActiveRagdoll/Assets/Scripts/BodyDetect.cs b/ActiveRagdoll/Assets/Scripts/BodyDetect.cs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyDetect.cs diff --git a/ActiveRagdoll/Assets/Scripts/BodyDetect.cs.meta b/ActiveRagdoll/Assets/Scripts/BodyDetect.cs.meta new file mode 100644 index 0000000..ee80127 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyDetect.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 113f3d270bd46e3418d34ea4ea4509aa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts.meta b/ActiveRagdoll/Assets/Scripts/BodyParts.meta new file mode 100644 index 0000000..afa6ba0 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 412d3da621067054a94fd5c1488bca2b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs b/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs new file mode 100644 index 0000000..a3d1009 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs @@ -0,0 +1,4 @@ +// 仅仅用来标记哪个rig是头部 +public class Head : BodyPart +{ +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs.meta b/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs.meta new file mode 100644 index 0000000..cf1940f --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/Head.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7bac41de18d827145842c71fd009a6f2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs b/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs new file mode 100644 index 0000000..fad2958 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs @@ -0,0 +1,28 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Hip : MonoBehaviour +{ + #region 搴忓垪鍖 + + #endregion + + #region 鍏叡瀛楁 + + #endregion + + #region 绉佹湁瀛楁 + + #endregion + + private void Awake() + { + // 绉佹湁瀛楁璧嬪 + + // 鍏叡瀛楁璧嬪 + + // 鍒濆鍖 + } + +} diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs.meta b/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs.meta new file mode 100644 index 0000000..8bb90d1 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/Hip.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6d2fa03cb7d9224468279869dc56f9a9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs b/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs new file mode 100644 index 0000000..2208869 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +// +public class BodyPart : MonoBehaviour +{ +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs.meta b/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs.meta new file mode 100644 index 0000000..514bd65 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/BodyParts/IBodyPart.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ce6b1ee2c847f9a4d80e2e5b04179f42 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs b/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs new file mode 100644 index 0000000..db3454e --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Threading; +using UnityEngine; + +/// <summary> +/// 角色状态和信息 +/// </summary> +[DefaultExecutionOrder(-1)] +public class CharacterInformation : MonoBehaviour +{ + + #region 公有字段 + [NonSerialized] public Transform rigRoot; + [NonSerialized] public Rigidbody[] rigs; + [NonSerialized] public Rigidbody head; + [NonSerialized] public bool isGrounded = false; + [NonSerialized] public float shortestDistanceFromHeadToGround; + [NonSerialized] public Vector3 landPosition; + + [NonSerialized] public float sinceGrounded; // 从离开地面开始计时 + [NonSerialized] public float sinceJumped; + [NonSerialized] public float sinceTurn; // + #endregion + + #region 私有字段 + private Transform m_HeadTransform; + private CheckForGroundCollision[] m_GroundCheckers; + #endregion + + private void Start() + { + rigRoot = transform.Find("Rigidbodies"); + rigs = rigRoot.gameObject.GetComponentsInChildren<Rigidbody>(); + head = rigRoot.Find("Head").GetComponent<Rigidbody>(); + + m_GroundCheckers = GetComponentsInChildren<CheckForGroundCollision>(); + m_HeadTransform = GetComponentInChildren<Head>().transform; + + shortestDistanceFromHeadToGround = float.MaxValue; + } + + public float GetTotalMass() + { + float mass = 0; + for(int i = 0; i < rigs.Length; ++i) + { + mass += rigs[i].mass; + } + return mass; + } + + void FixedUpdate() + { + sinceTurn += Time.deltaTime; + + isGrounded = false; + shortestDistanceFromHeadToGround = float.MaxValue; + + for (int i = 0; i < m_GroundCheckers.Length; i++) + { + if (m_GroundCheckers[i].isGrounded) + { + float dist = Vector3.Distance(m_HeadTransform.position, m_GroundCheckers[i].collisionPosition); + if (dist < shortestDistanceFromHeadToGround) + { + shortestDistanceFromHeadToGround = dist; + landPosition = m_GroundCheckers[i].collisionPosition; + } + isGrounded = true; + } + } + + if(!isGrounded) + { + sinceGrounded += Time.deltaTime; + } + else + { + sinceGrounded = 0; + } + } + + public float HeadRayCast() + { + Ray ray = new Ray(head.transform.position, Vector3.down); + RaycastHit mhit; + if (Physics.Raycast(ray, out mhit, 20, 1 << 8)) + { + float height = head.transform.position.y - mhit.point.y; + return height; + } + return shortestDistanceFromHeadToGround; + } + + private void OnDrawGizmos() + { + //Gizmos.DrawSphere(m_HeadTransform.position, 0.1f); + //Gizmos.DrawSphere(landPosition, 0.1f); + } + +} diff --git a/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs.meta b/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs.meta new file mode 100644 index 0000000..4d9328d --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/CharacterInformation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5740205186bdf76449e9e0fead6546ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs b/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs new file mode 100644 index 0000000..055f1ac --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CheckForGroundCollision : MonoBehaviour +{ + [NonSerialized] public bool isGrounded; + [NonSerialized] public Vector3 collisionPosition; + [NonSerialized] public Vector3 collisionNormal; + [NonSerialized] public Collider collider; + + private bool sensorGround; + + private void FixedUpdate() + { + // 根据上一次物理模拟(Physics Update)结果设置isGround参数 + if(!sensorGround) + { + isGrounded = false; + } + sensorGround = false; + } + + void Collide(Collision collision) + { + if(collision.transform.root == transform.root) // 自己碰撞自己 + { + return; + } + + float surfaceAngel = Vector3.Angle(Vector3.up, collision.contacts[0].normal); + + collider = collision.collider; + + if (surfaceAngel > 75f) // wall + { + + } + else // ground + { + sensorGround = true; + isGrounded = true; + collisionPosition = collision.contacts[0].point; + collisionNormal = collision.contacts[0].normal; + } + } + + public void OnCollisionEnter(Collision collision) + { + Collide(collision); + } + + public void OnCollisionStay(Collision collision) + { + Collide(collision); + } + +} diff --git a/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs.meta b/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs.meta new file mode 100644 index 0000000..d0cfb89 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/CheckForGroundCollision.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c07854afe58ee6c4d97210d9a51c366b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/JointAnimation.cs b/ActiveRagdoll/Assets/Scripts/JointAnimation.cs new file mode 100644 index 0000000..a38dc50 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointAnimation.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class JointAnimation : MonoBehaviour +{ + #region 搴忓垪鍖 + public new string name; + public JointMotion[] motions; + public Rigidbody hips; + + //public bool auto = false; + #endregion + + #region 鍏叡瀛楁 + + #endregion + + #region 绉佹湁瀛楁 + + #endregion + + private void Awake() + { + // 绉佹湁瀛楁璧嬪 + + // 鍏叡瀛楁璧嬪 + + // 鍒濆鍖 + } + + public void Start() + { + } + + private void FixedUpdate() + { + } + + public void Animate() + { + for(int i = 0; i < motions.Length; ++i) + { + motions[i].Animate(hips); + } + } + +} diff --git a/ActiveRagdoll/Assets/Scripts/JointAnimation.cs.meta b/ActiveRagdoll/Assets/Scripts/JointAnimation.cs.meta new file mode 100644 index 0000000..049720f --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointAnimation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fc08bc60c93c5614abaa96793e36f1c0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/JointMotion.cs b/ActiveRagdoll/Assets/Scripts/JointMotion.cs new file mode 100644 index 0000000..50d043f --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointMotion.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; + +[Serializable] +public class MotionCondition : SerializableCallback<bool> { } + +[Serializable] +public class JointMotion +{ + #region 搴忓垪鍖 + public string desc; + public JointMotionInfo info; + public MotionCondition condition; + #endregion + + #region 绉佹湁瀛楁 + public Rigidbody m_Rig; + #endregion + + public void Animate(Rigidbody hip) + { + if (condition.target != null && condition.Invoke() == false) + return; + + Vector3 dir = Vector3.zero; + + if (info.direction == JointMotionInfo.MoveDirection.Swing) + { + dir = m_Rig.transform.right; + } + + if (info.direction == JointMotionInfo.MoveDirection.FollowHip) + { + dir = hip.velocity.normalized; + } + + if (info.type == JointMotionInfo.MotionType.Torque) + { + m_Rig.AddTorque(dir * Time.deltaTime * info.forceMultiplier, ForceMode.Acceleration); + } + + if (info.type == JointMotionInfo.MotionType.Force) + { + m_Rig.AddForce(dir * Time.deltaTime * info.forceMultiplier, ForceMode.Acceleration); + } + } + +} diff --git a/ActiveRagdoll/Assets/Scripts/JointMotion.cs.meta b/ActiveRagdoll/Assets/Scripts/JointMotion.cs.meta new file mode 100644 index 0000000..f05be76 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointMotion.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 89d664c2adc472349866fa2dbdc6ff27 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs b/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs new file mode 100644 index 0000000..7c7ed4a --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[Serializable] +public class JointMotionInfo +{ + public enum MotionType + { + Torque, // drived by torque + Force, // drived by force + } + + public enum MoveDirection + { + FollowHip, // 鍜岃函骞叉柟鍚戜竴鑷 + Swing, // 鍗曠嫭鎽嗗姩 + } + + public MotionType type; + public MoveDirection direction; + + public float forceMultiplier; + +} diff --git a/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs.meta b/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs.meta new file mode 100644 index 0000000..4140531 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/JointMotionInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a14c5cc6013aca948a0f4d3be9c2d90b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/MatColor.cs b/ActiveRagdoll/Assets/Scripts/MatColor.cs new file mode 100644 index 0000000..a12c653 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/MatColor.cs @@ -0,0 +1,20 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +public class MatColor : MonoBehaviour +{ + public Color color; + + + private void Start() + { + var renderer = GetComponent<Renderer>(); + if(renderer != null) + { + renderer.material.color = color; + } + + } +} diff --git a/ActiveRagdoll/Assets/Scripts/MatColor.cs.meta b/ActiveRagdoll/Assets/Scripts/MatColor.cs.meta new file mode 100644 index 0000000..1c661c9 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/MatColor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0b91311883bc698469568f846e3d8e8d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActiveRagdoll/Assets/Scripts/PlayerController.cs b/ActiveRagdoll/Assets/Scripts/PlayerController.cs new file mode 100644 index 0000000..21eaa01 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/PlayerController.cs @@ -0,0 +1,315 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting; +using UnityEngine; +using UnityEngine.Scripting.APIUpdating; + +/// <summary> +/// 角色控制器 +/// </summary> +public class PlayerController : MonoBehaviour +{ + #region 序列化 + public Rigidbody[] m_RigsToLift; + public ConstantForce m_LeftKneeForce; + public ConstantForce m_RightKneeForce; + [Header("Gravity")] + public Vector3 m_ExtraGravity; + [Header("TurnBody")] + public float m_TorqueForce; + [Header("Stand")] + public float m_StandHeight; + public float m_StandUpForceMultiplier; + public AnimationCurve m_StandUpForceCurve; + [Header("Move")] + public float m_HorizontalMoveForce; + public float m_JumpForce; + public float m_SwitchLegInterval; + public float m_LegAngle; + public float m_TorsoForce; + [Header("动画")] + public JointAnimation m_MoveAnimation; + #endregion + + #region 公有字段 + [NonSerialized] public CharacterInformation info; + + public Vector3 forward + { + get + { + return -transform.forward; + } + } + + #endregion + + #region 私有字段 + private Rigidbody[] m_AllRigidBodies; + private Rigidbody m_HipRigidBody; + private Rigidbody m_HeadRigidBody; + private Rigidbody m_TorsoRigidBody; + private Rigidbody m_LegLeftRigidBody; + private Rigidbody m_LegRightRigidBody; + + private Quaternion m_TowardDirection; // 朝向角度 + + private bool m_LastMove; + private bool m_DoJump; + private bool m_DoMoveLeft; + private bool m_DoMoveRight; + + private float m_StepSwitchTimer; + private bool m_IsLeftForward; + + private float m_FallenGravityFactor; + + #endregion + + private void Awake() + { + InitRigidBodies(); + + m_HipRigidBody = transform.Find("Rigidbodies/Hip").GetComponent<Rigidbody>(); + m_HeadRigidBody = transform.Find("Rigidbodies/Head").GetComponent<Rigidbody>(); + m_TorsoRigidBody = transform.Find("Rigidbodies/Torso").GetComponent<Rigidbody>(); + m_LegLeftRigidBody = transform.Find("Rigidbodies/Leg_Left").GetComponent<Rigidbody>(); + m_LegRightRigidBody = transform.Find("Rigidbodies/Leg_Right").GetComponent<Rigidbody>(); + + info = GetComponent<CharacterInformation>(); + + m_TowardDirection = Quaternion.Euler(0, -90, 0); + m_DoJump = false; + m_LastMove = false; + } + + private void Update() + { + m_DoMoveLeft = false; + m_DoMoveRight = false; + + if (Input.GetKey(KeyCode.A)) + { + m_TowardDirection = Quaternion.Euler(0, -90, 0); + } + if (Input.GetKey(KeyCode.D)) + { + m_TowardDirection = Quaternion.Euler(0, 90, 0); + } + if (Input.GetKeyDown(KeyCode.W)) + { + m_DoJump = true; + } + if (Input.GetKey(KeyCode.A)) + { + m_DoMoveLeft = true; + } + if (Input.GetKey(KeyCode.D)) + { + m_DoMoveRight = true; + } + + m_StepSwitchTimer += Time.deltaTime; + if(m_StepSwitchTimer > m_SwitchLegInterval && Vector3.Angle(m_LegLeftRigidBody.transform.up, m_LegRightRigidBody.transform.up) > m_LegAngle) + { + m_IsLeftForward = !m_IsLeftForward; + m_StepSwitchTimer = m_StepSwitchTimer - m_SwitchLegInterval; + } + } + + private void FixedUpdate() + { + StandUp(); + TurnBody(); + OnMove(); + } + + private void OnMove() + { + if (!(m_DoMoveLeft || m_DoMoveRight || m_DoJump)) + { + m_LastMove = false; + return; + } + + //if (info.sinceTurn != 0) + //{ + // return; + //} + + if (!m_LastMove) // enter move + { + m_StepSwitchTimer = 0; + + m_LastMove = true; + } + + m_MoveAnimation.Animate(); + + if(m_DoMoveLeft) + { + MoveLeft(); + } + if (m_DoMoveRight) + { + MoveRight(); + } + if (m_DoJump) + { + Jump(); + m_DoJump = false; + } + } + + void MoveLeft() + { + DoActionToAllRigidbodies((rig, i) => { + rig.AddForce(Vector3.left * m_HorizontalMoveForce * Time.fixedDeltaTime, ForceMode.Acceleration); + }); + m_TorsoRigidBody.AddForce(Vector3.left * m_TorsoForce, ForceMode.Force); + } + + void MoveRight() + { + DoActionToAllRigidbodies((rig, i) => + { + rig.AddForce(Vector3.right * m_HorizontalMoveForce * Time.fixedDeltaTime, ForceMode.Acceleration); + }); + m_TorsoRigidBody.AddForce(Vector3.right * m_TorsoForce, ForceMode.Force); + } + + void Jump() + { + if(info.isGrounded || info.sinceGrounded < 0.2f) + { + Debug.Log("Jump"); + DoActionToAllRigidbodies((rig) => { + rig.AddForce(Vector3.up * m_JumpForce * Time.fixedDeltaTime, ForceMode.VelocityChange); + }); + } + } + + private void StandUp() + { + if (!info.isGrounded) // air + { + m_FallenGravityFactor += Time.fixedDeltaTime; + //m_FallenGravityFactor = Mathf.Clamp(m_FallenGravityFactor, 0, 1); + + ApplyExtraGravity(); + } + else // grounded + { + m_FallenGravityFactor = 0; + + //Vector3 standUpForce = m_StandUpForce * Vector3.up * (m_StandHeight - info.shortestDistanceFromHeadToGround); + float headHeight = info.HeadRayCast(); + if(headHeight < m_StandHeight) + { + float t = 1 - (m_StandHeight - headHeight) / m_StandHeight; + t = Mathf.Clamp(t, 0, 1); + //Debug.Log(t); + Vector3 standUpForce = m_StandUpForceMultiplier * m_StandUpForceCurve.Evaluate(t) * Vector3.up; + for (int i = 0; i < m_RigsToLift.Length; i++) + { + m_RigsToLift[i].AddForce(info.GetTotalMass() * standUpForce + info.GetTotalMass() * -Physics.gravity, ForceMode.Force); + } + } + } + + if(info.sinceGrounded < 0.3f) + { + m_LeftKneeForce.enabled = true; + m_RightKneeForce.enabled = true; + } + else + { + m_LeftKneeForce.enabled = false; + m_RightKneeForce.enabled = false; + } + } + + private void TurnBody() + { + Vector3 properForward = m_TowardDirection * -Vector3.forward; + float angel = Vector3.Angle(m_HipRigidBody.transform.forward, properForward); + Vector3 dir = Vector3.Cross(m_HipRigidBody.transform.forward, properForward); + if(Mathf.Abs(angel) < 5f) + { + info.sinceTurn = 0; + } + if(dir.y > 0) + { + m_HipRigidBody.AddTorque(Vector3.up * angel * m_TorqueForce, ForceMode.Acceleration) ; + } + else if(dir.y < 0) + { + m_HipRigidBody.AddTorque(-Vector3.up * angel * m_TorqueForce, ForceMode.Acceleration); + } + } + + /// <summary> + /// 初始化rigidbody + /// </summary> + private void InitRigidBodies() //初始化刚体,编号 + { + this.m_AllRigidBodies = base.GetComponentsInChildren<Rigidbody>(); + DoActionToAllRigidbodies((rig, i) => { + }); + + IgnoreCollision(); + } + + /// <summary> + /// 屏蔽身体各部件之间的碰撞 + /// </summary> + public void IgnoreCollision() + { + for (int i = 0; i < m_AllRigidBodies.Length - 1; i++) + { + for (int j = i + 1; j < m_AllRigidBodies.Length; j++) + { + Collider collider1 = m_AllRigidBodies[i].GetComponent<Collider>(); + Collider collider2 = m_AllRigidBodies[j].GetComponent<Collider>(); + Physics.IgnoreCollision(collider1, collider2, true); + } + } + } + + private void ApplyExtraGravity() + { + DoActionToAllRigidbodies((rig) => { + rig.AddForce(m_ExtraGravity * m_FallenGravityFactor, ForceMode.Acceleration); + }); + } + + private void DoActionToAllRigidbodies(Action<Rigidbody> action) + { + DoActionToAllRigidbodies((rig, i) => { + action(rig); + }); + } + + private void DoActionToAllRigidbodies(Action<Rigidbody, int> action) + { + for (int i = 0; i < m_AllRigidBodies.Length; ++i) + { + if (action != null) + { + action(m_AllRigidBodies[i], i); + } + } + } + + public bool StepLeft() + { + return m_IsLeftForward; + } + + public bool StepRight() + { + return !m_IsLeftForward; + } + +}
\ No newline at end of file diff --git a/ActiveRagdoll/Assets/Scripts/PlayerController.cs.meta b/ActiveRagdoll/Assets/Scripts/PlayerController.cs.meta new file mode 100644 index 0000000..00b10d4 --- /dev/null +++ b/ActiveRagdoll/Assets/Scripts/PlayerController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0dd98854264d2d4291220703c9d188e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |