123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
-
- using UnityEngine;
- using System.Collections;
- public class TankBehaviour : MonoBehaviour {
- private const float minMoveCheck=0.2f;
-
- public int groupId=0;
- public float moveSpeed=5, rotateSpeed=20;
-
- public Vector3 position{
- get{return transform.position;}
- }
-
- public Vector3 movement{
- get{return myMovement;}
- }
-
- private Vector3 myMovement=Vector3.zero;
- private TankGroup myGroup;
- private float tgtSpeed=0, speed=0, currentSpeed;
-
- public void SetGroup(int index){
- myGroup=TankGroup.GetTankGroup(index);
- }
-
-
- void Start () {
-
- }
- private void OnEnable()
- {
- SetGroup(groupId);
- }
-
- private void ondis()
- {
-
- }
-
- void Update () {
-
-
-
-
- Vector3 displacement=myGroup.targetPosition-position;
- Vector3 direction=displacement.normalized*myGroup.targetWeight;
-
- if(displacement.magnitude<myGroup.targetCloseDistance)
- direction*=displacement.magnitude/myGroup.targetCloseDistance;
-
- direction+=GetGroupMovement();
-
- if((myGroup.targetPosition-position).magnitude<minMoveCheck)
- tgtSpeed=0;
- else
- tgtSpeed=moveSpeed;
-
- speed=Mathf.Lerp(speed,tgtSpeed,2*Time.deltaTime);
-
- Drive(direction, speed);
- }
-
- private Vector3 GetGroupMovement(){
- Collider[] c=Physics.OverlapSphere(position,myGroup.keepDistance,myGroup.mask);
- Vector3 dis,v1=Vector3.zero, v2=Vector3.zero;
- for(int i=0; i<c.Length; i++){
- TankBehaviour otherTank=c[i].GetComponent<TankBehaviour>();
- dis=position-otherTank.position;
- v1+=dis.normalized*(1-dis.magnitude/myGroup.keepDistance);
- v2+=otherTank.movement;
-
- Debug.DrawLine(position, otherTank.position, Color.yellow);
- }
-
- return v1.normalized*myGroup.keepWeight+v2.normalized*myGroup.moveWeight;
- }
-
- private void Drive(Vector3 direction, float spd){
- Vector3 finialDirection=direction.normalized;
- float finialSpeed=spd, finialRotate=0;
- float rotateDir=Vector3.Dot(finialDirection,transform.right);
- float forwardDir=Vector3.Dot(finialDirection,transform.forward);
-
- if(forwardDir<0)
- rotateDir=Mathf.Sign(rotateDir);
-
- if(forwardDir<-0.2f)
- finialSpeed=Mathf.Lerp(currentSpeed,-spd*8,4*Time.deltaTime);
-
- if(forwardDir<0.98f)
- finialRotate=Mathf.Clamp(rotateDir*180,-rotateSpeed, rotateSpeed);
-
- finialSpeed*=Mathf.Clamp01(direction.magnitude);
- finialSpeed*=Mathf.Clamp01(1-Mathf.Abs(rotateDir)*0.8f);
-
- transform.Translate(Vector3.forward*finialSpeed*Time.deltaTime);
- transform.Rotate(Vector3.up*finialRotate*Time.deltaTime);
-
- currentSpeed=finialSpeed;
- myMovement=direction*finialSpeed;
- }
-
- }
|