TankBehaviour.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //write by shadow
  2. using UnityEngine;
  3. using System.Collections;
  4. public class TankBehaviour : MonoBehaviour {
  5. private const float minMoveCheck=0.2f;
  6. public int groupId=0;//组 id
  7. public float moveSpeed=5, rotateSpeed=20;//移动旋转速度
  8. public Vector3 position{
  9. get{return transform.position;}
  10. }
  11. public Vector3 movement{
  12. get{return myMovement;}
  13. }
  14. private Vector3 myMovement=Vector3.zero;
  15. private TankGroup myGroup;
  16. private float tgtSpeed=0, speed=0, currentSpeed;
  17. public void SetGroup(int index){
  18. myGroup=TankGroup.GetTankGroup(index);
  19. }
  20. // Use this for initialization
  21. void Start () {
  22. //SetGroup(groupId);
  23. }
  24. private void OnEnable()
  25. {
  26. SetGroup(groupId);
  27. }
  28. private void ondis()
  29. {
  30. }
  31. // Update is called once per frame
  32. void Update () {
  33. //if (myGroup == null)
  34. //{
  35. // return;
  36. //}
  37. Vector3 displacement=myGroup.targetPosition-position;//获取目标距离
  38. Vector3 direction=displacement.normalized*myGroup.targetWeight;//方向*权重
  39. if(displacement.magnitude<myGroup.targetCloseDistance)//重新计算目的地距离权重
  40. direction*=displacement.magnitude/myGroup.targetCloseDistance;
  41. direction+=GetGroupMovement();//获取周围组的移动
  42. if((myGroup.targetPosition-position).magnitude<minMoveCheck)//计算移动速度
  43. tgtSpeed=0;
  44. else
  45. tgtSpeed=moveSpeed;
  46. speed=Mathf.Lerp(speed,tgtSpeed,2*Time.deltaTime);
  47. Drive(direction, speed);//移动
  48. }
  49. private Vector3 GetGroupMovement(){
  50. Collider[] c=Physics.OverlapSphere(position,myGroup.keepDistance,myGroup.mask);//获取周围成员
  51. Vector3 dis,v1=Vector3.zero, v2=Vector3.zero;
  52. for(int i=0; i<c.Length; i++){
  53. TankBehaviour otherTank=c[i].GetComponent<TankBehaviour>();
  54. dis=position-otherTank.position;//距离
  55. v1+=dis.normalized*(1-dis.magnitude/myGroup.keepDistance);//查看与周围单位的距离
  56. v2+=otherTank.movement;//查看周围单位移动方向
  57. Debug.DrawLine(position, otherTank.position, Color.yellow);
  58. }
  59. return v1.normalized*myGroup.keepWeight+v2.normalized*myGroup.moveWeight;//添加权重因素
  60. }
  61. private void Drive(Vector3 direction, float spd){
  62. Vector3 finialDirection=direction.normalized;
  63. float finialSpeed=spd, finialRotate=0;
  64. float rotateDir=Vector3.Dot(finialDirection,transform.right);
  65. float forwardDir=Vector3.Dot(finialDirection,transform.forward);
  66. if(forwardDir<0)
  67. rotateDir=Mathf.Sign(rotateDir);
  68. if(forwardDir<-0.2f)
  69. finialSpeed=Mathf.Lerp(currentSpeed,-spd*8,4*Time.deltaTime);
  70. if(forwardDir<0.98f)//防抖
  71. finialRotate=Mathf.Clamp(rotateDir*180,-rotateSpeed, rotateSpeed);
  72. finialSpeed*=Mathf.Clamp01(direction.magnitude);
  73. finialSpeed*=Mathf.Clamp01(1-Mathf.Abs(rotateDir)*0.8f);
  74. transform.Translate(Vector3.forward*finialSpeed*Time.deltaTime);
  75. transform.Rotate(Vector3.up*finialRotate*Time.deltaTime);
  76. currentSpeed=finialSpeed;
  77. myMovement=direction*finialSpeed;
  78. }
  79. }