Files
taptap2024_GJ_chidouren/Assets/Scripts/Game/Component/PlayerEntity.cs

49 lines
2.1 KiB
C#
Raw Normal View History

2024-10-19 03:04:15 +08:00
using Framework.Timer;
using Framework.Utils.Extend;
2024-10-16 16:43:53 +08:00
using UnityEngine;
2024-10-16 00:03:41 +08:00
namespace Game.Component
{
public class PlayerEntity : MonoBehaviour
{
2024-10-19 03:04:15 +08:00
public float speed = 10;
2024-10-16 16:43:53 +08:00
public Rigidbody2D rigidbody2D;
2024-10-19 03:04:15 +08:00
private float speedBuffOffset;
private TimeHandler speedBuffTimer;
public float CurSpeed => this.speed *
(1 + (this.speedBuffTimer?.IsPlaying ?? false ? this.speedBuffOffset * (1 - this.speedBuffTimer.CurProgress) : 0));
public void AddSpeedBuff (float duration = 3 , float offset = 0.5f)
{
this.speedBuffOffset = offset;
this.speedBuffTimer?.Kill ();
this.speedBuffTimer = GameUpdateMgr.Instance.CreateTimer (duration , null);
}
2024-10-16 00:03:41 +08:00
public void OnMove (Vector2 vector)
{
2024-10-16 16:43:53 +08:00
var t = this.transform;
// 获取前方方向的世界坐标
2024-10-19 03:04:15 +08:00
Vector3 forward = (Vector3)vector;
2024-10-16 16:43:53 +08:00
//根据遥感向量获取一个01的移动速度系数
2024-10-19 03:04:15 +08:00
float speedOffset = forward.magnitude;
2024-10-16 16:43:53 +08:00
Quaternion targetRotation = Quaternion.FromToRotation (Vector3.up , forward.normalized);
transform.rotation = Quaternion.Lerp (transform.rotation , targetRotation , 0.25f);
// Debug.DrawLine (transform.position , transform.position + forward , Color.magenta);
2024-10-19 03:04:15 +08:00
this.transform.Translate ( Vector3.up * speedOffset * CurSpeed * Time.deltaTime);
2024-10-17 00:46:27 +08:00
// this.rigidbody2D.MovePosition (t.position + ((Vector3)vector * speed * Time.deltaTime));
2024-10-16 16:43:53 +08:00
// Vector3 v = target.position - transform.position;
// v.z = 0; // 确保向量在2D平面上
// float angle = Vector3.SignedAngle(Vector3.up, v, Vector3.forward);
// Quaternion rotation = Quaternion.Euler(0, 0, angle);
t.position = t.position.SetZ (0);
}
public void OnMoveEnd ()
{
this.transform.rotation = Quaternion.Euler (0 , 0 , this.transform.rotation.eulerAngles.z);
2024-10-16 00:03:41 +08:00
}
}
}