| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MoveCharacter : Character
- {
- public Foot foot;
- public float extraRiseGravity = 0; //上升时额外重力加速度
- public float extraFallGravity = -10; //下落时额外重力加速度
- public float moveSpeed = 5;
- //[HideInInspector]
- public float beRepelValue;
- public float totalBeRepelValue;
- [HideInInspector]
- public float weakTime;
- public float totalWeakTime;
- public float weakUpSpeed = 10f;
- public float decelerationRatio = 1f;
- public float minHurtKeepTime = 0.2f;
- [HideInInspector]
- public float hurtKeepTime = 0;
- public float hurtChangeVelocity = 1;
- [Header("新增漂浮效果参数")]
- public float maxTime = 0; //上升最大耗时
- public float minTime = 6; //上升最小耗时
- public float maxHeight = 12; //最大上升高度
- public float minHeight = 7; //最小上升高度
- public float maxRotateSpeed = 20; //最大旋转速度
- public float minRotateSpeed = 5; //最小旋转速度
- public float floatTime = 20; //漂浮时间
- private float curTime; //漂浮已进行时长
- private float height; //漂浮高度
- private float riseTime; //上升时间
- private float curHeight; //当前所在高度
- private float rotateSpeed; //旋转速度
- private float rotateDir; //旋转方向
- private Vector3 origPos; //初始位置
- private float rise = 1;
- private float down = -1;
- private Vector3 oneClockwise = new Vector3(0, 0, 1);
- private Vector3 oneCounterClockwise = new Vector3(0, 0, -1);
- private int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
- private GameObject spine;
- private MeshRenderer mesh;
- private Material[] mats;
- public Material[] outlineMats;
- private void Awake()
- {
- spine = transform.GetChild(0).GetChild(0).gameObject;
- mesh = spine.GetComponent<MeshRenderer>();
- mats = mesh.materials;
- }
- private void ChangeMat(int state)
- {
- if (spine == null || mesh == null || mats == null)
- {
- spine = transform.GetChild(0).GetChild(0).gameObject;
- mesh = spine.GetComponent<MeshRenderer>();
- mats = mesh.materials;
- }
- if (state == 0)
- {
- print(1);
- mesh.materials = outlineMats;
- }
- else
- {
- mesh.materials = mats;
- }
- }
- public void FloatStateOn()
- {
- ChangeMat(0);
- ChangeState(CharacterState.Rise);
- floatState = 1;
- riseTime = Random.Range(minTime, maxTime);
- origPos = transform.position;
- curHeight = origPos.y;
- curTime = 0;
- rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
- rotateDir = (1.5f - Random.Range(1, 3)) * 2;
- height = Random.Range(minHeight, maxHeight);
- }
- private void RotateSelf()
- {
- //transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
- }
- private void Update()
- {
- //print(maxTime);
- if (floatState == 1)
- {
- RotateSelf();
- curTime += Time.deltaTime;
- aniCollider.Play("Rise", 0, 0);
- curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
- transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- if (curHeight >= height - 0.02f)
- {
- floatState = 2;
- ChangeState(CharacterState.Float);
- transform.position = new Vector3(origPos.x, height, origPos.z);
- }
- }
- else if (floatState == 2)
- {
- RotateSelf();
- curTime += Time.deltaTime;
- transform.position = new Vector3(origPos.x, height, origPos.z);
- if (curTime >= floatTime)
- {
- floatState = 3;
- ChangeState(CharacterState.Fall);
- }
- }
- else if (floatState == 3)
- {
- RotateSelf();
- aniCollider.Play("Fall", 0, 0);
- if (transform.position.y >= origPos.y + 0.05f)
- {
- curHeight -= 10 * Time.deltaTime;
- transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- }
- else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
- {
- transform.position = origPos;
- floatState = 0;
- ChangeState(CharacterState.Idle);
- ChangeMat(1);
- foreach (Material m in mats)
- {
- m.SetInt("_Outline", 0);
- }
- if (rotateDir==1)
- {
- transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneClockwise, 0.1f);
- }
- else
- {
- transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneCounterClockwise, 0.1f);
- }
- if (transform.localEulerAngles.z >= -0.02f && transform.localEulerAngles.z <= 0.02f)
- {
- transform.localEulerAngles = new Vector3(0, 0, 0);
-
- }
- }
- }
- }
- public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
- {
- print("MoveCharacterBeHit");
- if (invincibleTime > 0)
- {
- return;
- }
- hp -= damage;
- uiHp.Show(hp, totalHp);
- if (hp <= 0)
- {
- rb.AddForce(force);
- ChangeState(CharacterState.Die);
- return;
- }
- else if (changeHurt && state == CharacterState.Weak)
- {
- print("ChangeHurt");
- rb.AddForce(force);
- ChangeState(CharacterState.Hurt);
- beRepelValue = totalBeRepelValue;
- return;
- }
- beRepelValue -= repelValue;
- if (changeHurt && beRepelValue <= 0)
- {
- print("ChangeWeak");
- ChangeState(CharacterState.Weak);
- }
- }
- }
|