首页 > 分享 > 实现宠物平滑跟随

实现宠物平滑跟随

实现宠物平滑跟随

最新推荐文章于 2023-11-10 12:51:18 发布

mq_shouhug753951mq 于 2016-08-26 13:34:23 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

using UnityEngine; using System.Collections; /// <summary> /// 宠物逻辑控制 /// </summary> public class PetController:MonoBehaviour { public Transform target; public float distance =1.7f; public float height = 1.0f; public float heightDamping = 0.5f; public float rotationDamping = 0.3f; private float _RefRotation = 0f; private float _RefHigh = 0f; private GameObject mRole;//当前角色 private NavMeshAgent agent; private GameObject mPetAni; private Animation Ani; public GameObject character; public Vector3 positionVector; public Vector3 lookVector; private SmoothFollowerObj posFollow; private SmoothFollowerObj lookFollow; private Vector3 lastVelocityDir; private Vector3 lastPos; void Start() { int id = PlayerPrefs.GetInt("RoleID"); Debug.Log(id); string UserName = PlayerDataManager.Instance.Attrib.Name; Debug.Log(UserName); agent = GetComponent<NavMeshAgent>(); agent.enabled = true; if (id == 1004) { character = GameObject.Find("Amazon(Clone)"); mRole = GameObject.Find("Amazon(Clone)"); //拿到当前角色的位置! target = mRole.transform; } if (id == 1003) { character = GameObject.Find("Warrior(Clone)"); mRole = GameObject.Find("Warrior(Clone)"); target = mRole.transform; } if (id == 1005) { character = GameObject.Find("Magician(Clone)"); mRole = GameObject.Find("Magician(Clone)"); target = mRole.transform; } if (id == 1006) { character = GameObject.Find("Wukong(Clone)"); mRole = GameObject.Find("Wukong(Clone)"); target = mRole.transform; } mPetAni = this.transform.GetChild(0).gameObject; Ani = mPetAni.GetComponent<Animation>(); Debug.Log(mPetAni.name); Debug.Log(mRole.transform.position); transform.LookAt(mRole.transform.position); transform.position = mRole.transform.position + new Vector3(0, 0, -1); positionVector = new Vector3(0, 0.5f, 1.7f); lookVector = new Vector3(0, 0, 1.5f); posFollow = new SmoothFollowerObj(0.5f, 0.5f); lookFollow = new SmoothFollowerObj(0.1f, 0.0f); posFollow.Update(transform.position, 0, true); lookFollow.Update(character.transform.position, 0, true); lastVelocityDir = character.transform.forward; lastPos = character.transform.position; } void FixedUpdate() { if (mRole != null) { float s = Vector3.Distance(transform.position, mRole.transform.position); if (Vector3.Distance(transform.position, mRole.transform.position) > 2.0f) { if (Application.loadedLevelName == "NewMain01") { // agent.destination = mRole.transform.position + (mRole.transform.position - transform.position); Debug.Log(mRole.transform.position); float a = Random.Range(1, -1); // transform.RotateAround(mRole.transform.position, Vector3.up, Time.deltaTime*180); PetMoveFllow(); } else { PetMoveFllow(); // this.transform.position = mRole.transform.position + new Vector3(0, 0, -1); } if (GameObject.Find("TJ(Clone)") != null) { PlayAni("tj_pao_01"); }else if (GameObject.Find("HYS(Clone)") != null) { PlayAni("hys_pao_01"); }else if (GameObject.Find("XSJ(Clone)") != null) { PlayAni("xsj_pao_01"); } //Ani.Play("tj_pao_01"); } else { if (GameObject.Find("TJ(Clone)") != null) { PlayAni("tj_daiji_01"); }else if (GameObject.Find("HYS(Clone)") != null) { PlayAni("hys_daiji_01"); }else if (GameObject.Find("XSJ(Clone)") != null) { PlayAni("xsj_daiji_01"); } } } transform.LookAt(mRole.transform.position, Vector3.up); } private void PetMoveFllow() { //if (!target) // return; //float wantedRotationAngle = target.eulerAngles.y; //float wantedHeight = target.position.y + height; //float currentRotationAngle = transform.eulerAngles.y; //float currentHeight = transform.position.y; //currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref _RefRotation, rotationDamping); //currentHeight = Mathf.SmoothDamp(currentHeight, wantedHeight, ref _RefHigh, heightDamping); //Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0); //transform.position = target.position; //transform.position -= currentRotation * Vector3.forward * distance; //transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z); //Debug.Log(" 场景名称:" + Application.loadedLevelName); lastVelocityDir += (character.transform.position - lastPos) * 8; lastPos = character.transform.position; lastVelocityDir += character.transform.forward * Time.deltaTime; lastVelocityDir = lastVelocityDir.normalized; Vector3 horizontal = transform.position - character.transform.position; Vector3 horizontal2 = horizontal; Vector3 vertical = character.transform.up; Vector3.OrthoNormalize(ref vertical, ref horizontal2); if (horizontal.sqrMagnitude > horizontal2.sqrMagnitude) horizontal = horizontal2; transform.position = posFollow.Update( character.transform.position + horizontal * Mathf.Abs(positionVector.z) + vertical * positionVector.y, Time.deltaTime ); horizontal = lastVelocityDir; Vector3 look = lookFollow.Update(character.transform.position + horizontal * lookVector.z - vertical * lookVector.y, Time.deltaTime); transform.rotation = Quaternion.FromToRotation(transform.forward, look - transform.position) * transform.rotation; } void PlayAni(string aniName) { if (aniName != null) { Ani.CrossFade(aniName,0.2f); } else { Debug.Log("不存在当前的宠物名称!"); } } } public class SmoothFollowerObj { private Vector3 targetPosition; private Vector3 position; private Vector3 velocity; private float smoothingTime; private float prediction; public SmoothFollowerObj(float smoothingTime) { targetPosition = Vector3.zero; position = Vector3.zero; velocity = Vector3.zero; this.smoothingTime = smoothingTime; prediction = 1; } public SmoothFollowerObj(float smoothingTime, float prediction) { targetPosition = Vector3.zero; position = Vector3.zero; velocity = Vector3.zero; this.smoothingTime = smoothingTime; this.prediction = prediction; } // Update should be called once per frame public Vector3 Update(Vector3 targetPositionNew, float deltaTime) { Vector3 targetVelocity = (targetPositionNew - targetPosition) / deltaTime; targetPosition = targetPositionNew; float d = Mathf.Min(1, deltaTime / smoothingTime); velocity = velocity * (1 - d) + (targetPosition + targetVelocity * prediction - position) * d; position += velocity * Time.deltaTime; return position; } public Vector3 Update(Vector3 targetPositionNew, float deltaTime, bool reset) { if (reset) { targetPosition = targetPositionNew; position = targetPositionNew; velocity = Vector3.zero; return position; } return Update(targetPositionNew, deltaTime); } public Vector3 GetPosition() { return position; } public Vector3 GetVelocity() { return velocity; } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240

相关知识

实现宠物平滑跟随
Unity实现宠物跟随移动
unity 实现宠物跟随移动
unity宠物跟随移动控制
unity宠物跟随移动控制资源
【UE功能实现】宠物跟随
Unity宠物跟随
UE4 人物跟随移动(宠物跟随)
宠物跟随
鼠标宠物跟随

网址: 实现宠物平滑跟随 https://m.mcbbbk.com/newsview455504.html

所属分类:萌宠日常
上一篇: 天涯明月刀宠物系统怎么玩
下一篇: 松鼠宠物怎么获得,剑网3指尖江湖