|
@@ -13,6 +13,7 @@ using UnityEngine.Rendering.PostProcessing;
|
|
|
* 控制宠物在Home场景动画
|
|
|
* !!!特别注意:Dog Initializer 必须挂载在同一个组件下,并且必须在本组价下方。确保比本组件先执行
|
|
|
* 主要调节参数在FixedUpdate代码段里面
|
|
|
+ * 提示用户注册
|
|
|
*/
|
|
|
|
|
|
public class HomeController : MonoBehaviour
|
|
@@ -26,6 +27,7 @@ public class HomeController : MonoBehaviour
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
void Start()
|
|
|
{
|
|
|
+
|
|
|
lastCameraChange = DateTime.Now;
|
|
|
playerCam = GameObject.Find("VCam Player").GetComponent<CinemachineVirtualCamera>();
|
|
|
dogCam = GameObject.Find("VCam Dog").GetComponent<CinemachineVirtualCamera>();
|
|
@@ -41,7 +43,7 @@ public class HomeController : MonoBehaviour
|
|
|
if (dateTime.Hour >= 22 || dateTime.Hour <= 5) // 深夜模式,狗默认在睡觉状态
|
|
|
{
|
|
|
dog.Sleep();
|
|
|
- }
|
|
|
+ }else if(dog.dogProperty.stamina <= 10) { dog.Sleep(); } // 狗体力太低了,进入睡觉模式
|
|
|
else
|
|
|
{
|
|
|
dog.StartAnimation();
|
|
@@ -53,10 +55,25 @@ public class HomeController : MonoBehaviour
|
|
|
// Update is called once per frame
|
|
|
void FixedUpdate()
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
#region 场景动画主循环
|
|
|
- // 生成一个数据数用于随机开启动画,如果和狗的randomFactor相同就开启动画
|
|
|
- int randomCheck = UnityEngine.Random.Range(0, 51);
|
|
|
+ // 检测狗是否被撞翻,如果是,立刻翻回来
|
|
|
+ foreach (var dog in dogsInScene)
|
|
|
+ {
|
|
|
+ Quaternion curRotation = dog.gameObject.transform.rotation;
|
|
|
+ if (curRotation.x !=0)
|
|
|
+ {
|
|
|
+ curRotation.x = 0;
|
|
|
+ }
|
|
|
+ if (curRotation.z != 0)
|
|
|
+ {
|
|
|
+ curRotation.z = 0;
|
|
|
+ }
|
|
|
+ dog.gameObject.transform.rotation = curRotation;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成一个数据数用于随机开启动画,如果和狗的randomFactor相同就开启动画
|
|
|
+ int randomCheck = UnityEngine.Random.Range(0, 51);
|
|
|
foreach (var dog in dogsInScene)
|
|
|
{
|
|
|
// 如果在eat drink进程结束前不执行随机场景代码
|
|
@@ -93,6 +110,7 @@ public class HomeController : MonoBehaviour
|
|
|
else // 狗狗开始步行移动
|
|
|
{
|
|
|
dog.SetMoveSpeed(0);
|
|
|
+ dog.moveSpeed = 0;
|
|
|
dog.Move();
|
|
|
}
|
|
|
}
|
|
@@ -111,9 +129,9 @@ public class HomeController : MonoBehaviour
|
|
|
//void InitialScene()
|
|
|
IEnumerator InitialScene()
|
|
|
{
|
|
|
- //
|
|
|
- yield return null; // 跳过第一帧
|
|
|
- Debug.Log("Home InitialScene is called");
|
|
|
+ //yield return null;
|
|
|
+ //yield return null;
|
|
|
+ yield return null; // 跳过三帧,初始化最多三只狗
|
|
|
Debug.Log(isInitialDone);
|
|
|
foreach (var dog in UserProperty.dogs)
|
|
|
{
|
|
@@ -123,7 +141,11 @@ public class HomeController : MonoBehaviour
|
|
|
float y = UnityEngine.Random.Range(0, 360f);
|
|
|
var initPosition = new Vector3(x, 0, z);
|
|
|
StartCoroutine(DogComponentAdd(dog)); // 加载狗的其他组件
|
|
|
- var dogGameObject = GameObject.Find(dog.name);
|
|
|
+ var dogGameObject = GameObject.Find(dog.dog_name);
|
|
|
+ if (dogGameObject == null)
|
|
|
+ {
|
|
|
+ Debug.Log(dog.dog_name + "is not found in Home Controller");
|
|
|
+ }
|
|
|
dogGameObject.transform.position = initPosition;
|
|
|
dogGameObject.transform.rotation = Quaternion.Euler(0, y, 0);
|
|
|
dogGameObject.transform.localScale = new Vector3(2, 2, 2);
|
|
@@ -139,7 +161,7 @@ public class HomeController : MonoBehaviour
|
|
|
yield return null;
|
|
|
|
|
|
// 第一帧以后开始执行
|
|
|
- GameObject dog = GameObject.Find(dogProperty.name);
|
|
|
+ GameObject dog = GameObject.Find(dogProperty.dog_name);
|
|
|
|
|
|
// 加载指定的Animator controller
|
|
|
Animator animator = dog.GetComponent<Animator>();
|
|
@@ -148,12 +170,24 @@ public class HomeController : MonoBehaviour
|
|
|
|
|
|
animator.runtimeAnimatorController = animatorController;
|
|
|
|
|
|
- // 加载bbx collider
|
|
|
+ // 加载Rigidbody
|
|
|
+ Rigidbody rigidbody = dog.AddComponent<Rigidbody>();
|
|
|
+ //rigidbody.isKinematic = true;
|
|
|
+ rigidbody.mass = 10;
|
|
|
+ rigidbody.linearDamping = 10;
|
|
|
+ rigidbody.angularDamping = 10;
|
|
|
+ //rigidbody.freezeRotation = true;
|
|
|
+ rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;
|
|
|
+ rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
|
|
|
+ rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
|
|
|
+
|
|
|
+ // 加载box collider
|
|
|
BoxCollider boxCollider = dog.AddComponent<BoxCollider>();
|
|
|
- boxCollider.isTrigger = true;
|
|
|
- boxCollider.center = new Vector3(0, 0.225f, 0);
|
|
|
- boxCollider.size = new Vector3(0.2f, 0.45f, 0.6f);
|
|
|
+ boxCollider.isTrigger = false;
|
|
|
+ boxCollider.center = new Vector3(0, 0.25f, 0);
|
|
|
+ boxCollider.size = new Vector3(0.12f, 0.45f, 0.54f);
|
|
|
|
|
|
+ //yield return null;
|
|
|
}
|
|
|
|
|
|
// 场景随机切换镜头看向不同的狗
|
|
@@ -164,7 +198,6 @@ public class HomeController : MonoBehaviour
|
|
|
if (ts.TotalSeconds < delay) {return;}
|
|
|
int dogCount = dogsInScene.Count;
|
|
|
int r = UnityEngine.Random.Range(0, dogCount+1);
|
|
|
- Debug.Log("RandomCameraChange Start. R:" + r);
|
|
|
if (r < dogCount)
|
|
|
{
|
|
|
dogCam.m_LookAt = dogsInScene[r].gameObject.transform;
|
|
@@ -188,19 +221,22 @@ public class DogInScene
|
|
|
public GameObject gameObject { set; get; }
|
|
|
public Animator animator;
|
|
|
private Vector3 moveToLocation;
|
|
|
- private float moveSpeed; // 用来控制狗的移动速度 0 0.5跑,0.75快跑,1跳
|
|
|
+ public float moveSpeed; // 用来控制狗的移动速度 0 0.5跑,0.75快跑,1跳
|
|
|
|
|
|
// 喝水吃饭参数段
|
|
|
public DateTime drinkStartTime, eatStartTime; // 记录吃喝开始时间
|
|
|
public bool itemConsumeProgress, eatProgress = false; // 是否在吃和喝的进程中。如果是的话,跳过常规动画检测
|
|
|
public bool isMovingToBowl;
|
|
|
|
|
|
+
|
|
|
// 随机动作参数段
|
|
|
- public int randomFactor = UnityEngine.Random.Range(0, 51); // 生成一个随机整数(0 到 50 之间),用于时间校验
|
|
|
+ public int randomFactor;
|
|
|
public DateTime animationStartTime; // 记录上一个动画开始时间的,每个随机动作间隔至少30秒
|
|
|
private int activeIndex; // 动物的活动指数
|
|
|
public bool isMoving; // 动物正在移动,避免其他随机动作发生
|
|
|
|
|
|
+ // 固定参数
|
|
|
+ private float moveSpeedAdj = 0.022f;
|
|
|
|
|
|
|
|
|
#region 通用函数段
|
|
@@ -208,7 +244,8 @@ public class DogInScene
|
|
|
{
|
|
|
this.gameObject = gameObject;
|
|
|
this.animator = gameObject.GetComponent<Animator>();
|
|
|
-
|
|
|
+ this.randomFactor = UnityEngine.Random.Range(0, 51); // 生成一个随机整数(0 到 50 之间),用于时间校验
|
|
|
+ Debug.Log(this.gameObject.name + "random factor is:"+randomFactor);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -225,10 +262,10 @@ public class DogInScene
|
|
|
{
|
|
|
this.animationStartTime = DateTime.Now;
|
|
|
this.animator.SetInteger("activeIndex", activeIndex);
|
|
|
- Debug.Log("activeIndex:" + this.activeIndex);
|
|
|
+ //Debug.Log("activeIndex:" + this.activeIndex);
|
|
|
float randomIndex = UnityEngine.Random.Range(0, 1f);
|
|
|
this.animator.SetFloat("randomIndex", randomIndex);
|
|
|
- Debug.Log("randomIndex:" + randomIndex);
|
|
|
+ //Debug.Log("randomIndex:" + randomIndex);
|
|
|
}
|
|
|
|
|
|
public void SetMoveSpeed(float speed)
|
|
@@ -238,12 +275,20 @@ public class DogInScene
|
|
|
|
|
|
public void Move()
|
|
|
{
|
|
|
+
|
|
|
+ // 如果距离目标小于0.5米就把速度调整为零
|
|
|
+ if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
|
|
|
+ {
|
|
|
+ //Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
|
|
|
+ this.SetMoveSpeed(0);
|
|
|
+ //Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
|
|
|
+ }
|
|
|
|
|
|
if (isMoving == false)
|
|
|
{
|
|
|
animationStartTime = DateTime.Now; // 设置动画开始时间
|
|
|
float x = UnityEngine.Random.Range(-5f, 5f);
|
|
|
- float z = UnityEngine.Random.Range(-5f, 5f);
|
|
|
+ float z = UnityEngine.Random.Range(0f, 5f);
|
|
|
this.moveToLocation = new Vector3(x, 0, z);
|
|
|
//Debug.Log("move to location:" + x + ", " + z);
|
|
|
this.isMoving = true;
|
|
@@ -252,7 +297,8 @@ public class DogInScene
|
|
|
this.animator.SetFloat("moveSpeed", this.moveSpeed);
|
|
|
}
|
|
|
this.gameObject.transform.LookAt(moveToLocation);
|
|
|
- this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * 0.02f * 0.01f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整
|
|
|
+ this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
|
|
|
+ //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100 + dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
|
|
|
//Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
|
|
|
|
|
|
// 如果狗距离到达重点就停止跑步动画
|
|
@@ -260,6 +306,7 @@ public class DogInScene
|
|
|
if (distance < 0.1)
|
|
|
{
|
|
|
this.animator.SetBool("isMoving", false);
|
|
|
+ //this.moveSpeed = 0.4f;
|
|
|
this.isMoving = false;
|
|
|
StartAnimation();
|
|
|
}
|
|
@@ -290,7 +337,7 @@ public class DogInScene
|
|
|
{
|
|
|
if (result)
|
|
|
{
|
|
|
- // TODO 成功,狗狗跑过来,
|
|
|
+ // TODO 语音信息识别成功,狗狗跑过来
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -304,15 +351,18 @@ public class DogInScene
|
|
|
// 开始整个使用道具的流程
|
|
|
public void StartItemConsume(ItemGroup group)
|
|
|
{
|
|
|
-
|
|
|
|
|
|
- GameObject bowl = new();
|
|
|
+ GameObject bowl = GameObject.Find("Bowl_water"); // 先指定一个碗,编译通过
|
|
|
+
|
|
|
+ this.itemConsumeProgress = true; // 开启使用道具过程
|
|
|
if (group == ItemGroup.water)
|
|
|
{
|
|
|
- this.itemConsumeProgress = true; // 开启整个喝水的进程
|
|
|
- bowl = GameObject.Find("Bowl_water");
|
|
|
+ bowl = GameObject.Find("Bowl_water"); // 开启整个喝水的进程
|
|
|
+ }
|
|
|
+ if (group == ItemGroup.food)
|
|
|
+ {
|
|
|
+ bowl = GameObject.Find("Bowl_food"); // 开启整个吃食物的进程
|
|
|
}
|
|
|
- if (group == ItemGroup.food) { } // 吃事物的过程
|
|
|
this.moveToLocation = bowl.transform.position;
|
|
|
this.isMovingToBowl = true;
|
|
|
this.animator.SetTrigger("move"); // 切换为走路动画
|
|
@@ -322,16 +372,23 @@ public class DogInScene
|
|
|
|
|
|
public void MovetoBowl()
|
|
|
{
|
|
|
- // 隐藏主菜单
|
|
|
- //var uiPlaceholder = GameObject.Find("UI Placeholder");
|
|
|
- //var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
|
|
|
+
|
|
|
+ // 如果距离目标小于0.5米就把速度调整为零
|
|
|
+ if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
|
|
|
+ {
|
|
|
+ Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
|
|
|
+ this.SetMoveSpeed(0);
|
|
|
+ Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
|
|
|
+ }
|
|
|
+
|
|
|
var vamUI = GameObject.Find("VoiceAndMenu");
|
|
|
if (vamUI != null)
|
|
|
{
|
|
|
vamUI.SetActive(false);
|
|
|
}
|
|
|
this.gameObject.transform.LookAt(moveToLocation);
|
|
|
- this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * 0.02f * 0.01f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整
|
|
|
+ this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
|
|
|
+ //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100+dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
|
|
|
//Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
|
|
|
|
|
|
// 如果狗距离到达重点就停止跑步动画
|
|
@@ -342,30 +399,17 @@ public class DogInScene
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //public IEnumerator MovetoBowl()
|
|
|
- //{
|
|
|
-
|
|
|
- // this.gameObject.transform.LookAt(moveToLocation);
|
|
|
- // // 如果狗距离到达重点就停止跑步动画
|
|
|
- // float distance = 1000;
|
|
|
- // while (distance > 0.1)
|
|
|
- // {
|
|
|
- // distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
|
|
|
- // this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * 0.02f * 0.01f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整
|
|
|
- // }
|
|
|
- // this.animator.SetBool("isMoving", false);
|
|
|
- //}
|
|
|
-
|
|
|
- //public void DrinkAnimation()
|
|
|
public IEnumerator DrinkAnimation()
|
|
|
|
|
|
{
|
|
|
this.animator.SetBool("isMoving", false); // 关闭移动动画
|
|
|
TimeSpan ts = new TimeSpan();
|
|
|
- var bowlWater = GameObject.Find("Bowl_water");
|
|
|
+ var targetBowl = GameObject.Find("Bowl_water");
|
|
|
+
|
|
|
+
|
|
|
|
|
|
// 摄像头看向水盆位置
|
|
|
- HomeController.dogCam.m_LookAt = bowlWater.transform;
|
|
|
+ HomeController.dogCam.m_LookAt = targetBowl.transform;
|
|
|
HomeController.dogCam.Priority = 10;
|
|
|
HomeController.playerCam.Priority = 1;
|
|
|
|
|
@@ -373,10 +417,18 @@ public class DogInScene
|
|
|
{
|
|
|
yield return new WaitForSeconds(0.25f);
|
|
|
ts = DateTime.Now - this.drinkStartTime;
|
|
|
- Debug.Log("结束饮水过程:" + ts.TotalSeconds);
|
|
|
+ //Debug.Log("结束饮水过程:" + ts.TotalSeconds);
|
|
|
+
|
|
|
+ // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
|
|
|
+ this.gameObject.transform.LookAt(targetBowl.transform.position);
|
|
|
}
|
|
|
// 播放10秒后结束饮水过程
|
|
|
this.animator.SetBool("isDrinking", false);
|
|
|
+
|
|
|
+ // 摄像头恢复玩家视角
|
|
|
+ HomeController.dogCam.Priority = 1;
|
|
|
+ HomeController.playerCam.Priority = 10;
|
|
|
+
|
|
|
//var water = GameObject.Find("Water");
|
|
|
//water.SetActive(false);
|
|
|
|
|
@@ -385,19 +437,58 @@ public class DogInScene
|
|
|
{
|
|
|
yield return new WaitForSeconds(0.25f);
|
|
|
ts = DateTime.Now - this.drinkStartTime;
|
|
|
- Debug.Log("让水盆消失:" + ts.TotalSeconds);
|
|
|
+ //Debug.Log("让水盆消失:" + ts.TotalSeconds);
|
|
|
}
|
|
|
|
|
|
- bowlWater.transform.position = new Vector3(-1, -10, -1); // 将喝水碗回归原位
|
|
|
+ targetBowl.transform.position = new Vector3(-1, -10, -1); // 将喝水碗回归原位
|
|
|
this.itemConsumeProgress = false; // 关闭整个喝水的进程
|
|
|
|
|
|
QuitItemConsume();
|
|
|
}
|
|
|
|
|
|
- public void Eat()
|
|
|
+ public IEnumerator EatAnimation()
|
|
|
{
|
|
|
- this.animator.SetBool("isEating", true);
|
|
|
- this.animator.SetBool("isMoving", false);
|
|
|
+ this.animator.SetBool("isMoving", false); // 关闭移动动画
|
|
|
+ TimeSpan ts = new TimeSpan();
|
|
|
+ var targetBowl = GameObject.Find("Bowl_food");
|
|
|
+
|
|
|
+
|
|
|
+ // 摄像头看向盆位置
|
|
|
+ HomeController.dogCam.m_LookAt = targetBowl.transform;
|
|
|
+ HomeController.dogCam.Priority = 10;
|
|
|
+ HomeController.playerCam.Priority = 1;
|
|
|
+
|
|
|
+ while (ts.TotalSeconds < 10)
|
|
|
+ {
|
|
|
+ yield return new WaitForSeconds(0.25f);
|
|
|
+ ts = DateTime.Now - this.eatStartTime;
|
|
|
+ //Debug.Log("结束饮水过程:" + ts.TotalSeconds);
|
|
|
+
|
|
|
+ // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
|
|
|
+ this.gameObject.transform.LookAt(targetBowl.transform.position);
|
|
|
+ }
|
|
|
+ // 播放10秒后结束饮水过程
|
|
|
+ this.animator.SetBool("isEating", false);
|
|
|
+
|
|
|
+ // 摄像头恢复玩家视角
|
|
|
+ HomeController.dogCam.Priority = 1;
|
|
|
+ HomeController.playerCam.Priority = 10;
|
|
|
+
|
|
|
+ //var water = GameObject.Find("Water");
|
|
|
+ //water.SetActive(false);
|
|
|
+
|
|
|
+ // 再等待几秒后让水盆消失
|
|
|
+ while (ts.TotalSeconds < 15)
|
|
|
+ {
|
|
|
+ yield return new WaitForSeconds(0.25f);
|
|
|
+ ts = DateTime.Now - this.drinkStartTime;
|
|
|
+ //Debug.Log("让水盆消失:" + ts.TotalSeconds);
|
|
|
+ }
|
|
|
+
|
|
|
+ targetBowl.transform.position = new Vector3(-1, -10, -1); // 将喝水碗回归原位
|
|
|
+ this.itemConsumeProgress = false; // 关闭整个喝水的进程
|
|
|
+
|
|
|
+ QuitItemConsume();
|
|
|
}
|
|
|
|
|
|
private void QuitItemConsume()
|
|
@@ -405,10 +496,10 @@ public class DogInScene
|
|
|
var uiPlaceholder = GameObject.Find("UI Placeholder");
|
|
|
var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
|
|
|
vamUI.SetActive(true);
|
|
|
+ this.moveSpeed = UnityEngine.Random.Range(0.3f,0.6f);
|
|
|
+ Move();
|
|
|
|
|
|
- // 摄像头恢复玩家视角
|
|
|
- HomeController.dogCam.Priority = 1;
|
|
|
- HomeController.playerCam.Priority = 10;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
#endregion
|