blob: db3454e0c75adf2eee56285414e2a1600d07398f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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);
}
}
|