You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.4 KiB
161 lines
4.4 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
|
|
public class Movement : MonoBehaviour |
|
{ |
|
public Rigidbody rigi; |
|
public bool MOVE = false; |
|
public bool useApplyRootMotion = false; |
|
[Header("이동 속도")] |
|
[Range(0, 10)] |
|
public float spdMove = 3f; |
|
[Header("회전 속도")] |
|
[Range(0, 5)] |
|
public float spdRotate = 1f; |
|
[Header("멈추게 될 거리")] |
|
[Range(0, 1)] |
|
public float disStop = 0.25f; |
|
[Space(10)] |
|
public bool USE_TIMER = false; |
|
[Range(0, 10)] |
|
public float timeAlive = 0.5f; |
|
|
|
public void Initialized(System.Action OnMoveEnd, bool useApplyRootMotion) |
|
{ |
|
this.OnMoveEnd = OnMoveEnd; |
|
this.useApplyRootMotion = useApplyRootMotion; |
|
|
|
StopRigibody(); |
|
} |
|
|
|
GameObject targetChase; |
|
System.Action OnMoveEnd; |
|
float timeSum = -1, timeSumPos = -1; |
|
Vector3 vecDir = Vector3.zero; |
|
Vector3 posStart = Vector3.zero; |
|
Vector3 posEnd = Vector3.zero; |
|
/// <summary> |
|
/// 쫓아갈 오브젝트로 목적지를 설정합니다. |
|
/// </summary> |
|
/// <param name="target"></param> |
|
/// <param name="OnMoveEnd"></param> |
|
/// <param name="useApplyRootMotion"></param> |
|
public void TargetSetting(GameObject target) |
|
{ |
|
targetChase = target; |
|
|
|
TargetSetting(targetChase.transform.position); |
|
} |
|
/// <summary> |
|
/// 이동할 목적지을 설정합니다. |
|
/// Animator 의 ApplyRootMotion 의 상태에 따라 이동 방식이 변경 됩니다. |
|
/// </summary> |
|
/// <param name="pos">도착 위치</param> |
|
/// <param name="OnMoveEnd">이동 완료 이벤트</param> |
|
/// <param name="useApplyRootMotion">Animator 의 ApplyRootMotion 상태</param> |
|
public float a = 0; |
|
public void TargetSetting(Vector3 pos) |
|
{ |
|
MOVE = true; |
|
|
|
posStart = transform.position; |
|
posEnd = pos; |
|
timeSum = 0; |
|
timeSumPos = 0; |
|
|
|
float dis = Vector3.Distance(posStart, posEnd); |
|
a = (Time.fixedDeltaTime * spdMove) / dis; |
|
} |
|
public void Stop(bool EVENT = false) |
|
{ |
|
MOVE = false; |
|
StopRigibody(); |
|
|
|
if(EVENT && OnMoveEnd != null) |
|
OnMoveEnd(); |
|
} |
|
void StopRigibody() |
|
{ |
|
if (rigi == null) |
|
return; |
|
|
|
rigi.velocity = Vector3.zero; |
|
} |
|
|
|
void FixedUpdate() |
|
{ |
|
//if (MOVE) |
|
//{ |
|
// if (targetChase != null && !targetChase.activeSelf) |
|
// { |
|
// Stop(); |
|
|
|
// return; |
|
// } |
|
|
|
// if (timeSum != -1) |
|
// { |
|
// timeSum += (Time.fixedDeltaTime * spdRotate); |
|
|
|
// vecDir = posEnd - transform.position; |
|
// vecDir.y = 0; |
|
|
|
// //transform.rotation = Quaternion.LookRotation(Vector3.Slerp(transform.forward, vecDir.normalized, timeSum), Vector3.up); |
|
|
|
// if (1 < timeSum) |
|
// { |
|
// timeSum = -1; |
|
|
|
// //타겟이 설정되어 있다면 방향과 거리는 항상 바뀔수 있음 |
|
// if (targetChase != null) |
|
// { |
|
// timeSum = 0; |
|
// posEnd = targetChase.transform.position; |
|
// } |
|
// } |
|
// } |
|
|
|
// if (!useApplyRootMotion) |
|
// { |
|
// if (rigi != null) |
|
// { |
|
// rigi.position += (vecDir.normalized * spdMove) * Time.fixedDeltaTime; |
|
// Debug.LogWarning($"{rigi.position}"); |
|
// } |
|
// else |
|
// { |
|
// transform.position = Vector3.LerpUnclamped(posStart, posEnd, timeSumPos += a); |
|
|
|
// if (1f < timeSumPos) |
|
// Stop(true); |
|
// } |
|
// } |
|
// else if (Vector3.Distance(transform.position, posEnd) < disStop) |
|
// { |
|
// Stop(true); |
|
// } |
|
//} |
|
|
|
if (Input.GetKey(KeyCode.A)) |
|
{ |
|
rigi.position += Vector3.left * spdMove; |
|
} |
|
else if (Input.GetKey(KeyCode.D)) |
|
{ |
|
rigi.position += Vector3.right * spdMove; |
|
} |
|
|
|
if (Input.GetKey(KeyCode.W)) |
|
{ |
|
rigi.position += Vector3.forward * spdMove; |
|
} |
|
else if (Input.GetKey(KeyCode.S)) |
|
{ |
|
rigi.position += Vector3.back * spdMove; |
|
} |
|
|
|
} |
|
[Space(20)] |
|
public bool TEST = false; |
|
}
|
|
|