Initial commit
This commit is contained in:
140
Assets/Scripting/Khac/AnimationControllerAPM.cs
Normal file
140
Assets/Scripting/Khac/AnimationControllerAPM.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System;
|
||||
|
||||
public class AnimationControllerAPM : MonoBehaviour
|
||||
{
|
||||
private Animator animator;
|
||||
private AnimationClip animationUClip;
|
||||
private AnimationClip animationDClip;
|
||||
|
||||
// Tham chiếu đến pallet và fork
|
||||
public Transform pallet; // Gán GameObject của pallet trong Inspector
|
||||
public Transform fork; // Gán GameObject của fork trong Inspector
|
||||
|
||||
public float AnimationUDuration => animationUClip != null ? animationUClip.length : 0f;
|
||||
public float AnimationDDuration => animationDClip != null ? animationDClip.length : 0f;
|
||||
|
||||
public Action<string, string> OnAnimationStateChanged { get; set; } // ActionType, State (RUNNING/FINISHED)
|
||||
|
||||
private string currentActionType; // Theo dõi ActionType hiện tại (pick/drop)
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
animator = GetComponent<Animator>();
|
||||
animator.SetBool("U", false);
|
||||
animator.SetBool("D", false);
|
||||
|
||||
InitializeAnimationClips();
|
||||
}
|
||||
|
||||
private void InitializeAnimationClips()
|
||||
{
|
||||
if (animator == null || animator.runtimeAnimatorController == null)
|
||||
{
|
||||
Debug.LogError("Animator hoặc AnimatorController không được gán!");
|
||||
return;
|
||||
}
|
||||
|
||||
var controller = animator.runtimeAnimatorController;
|
||||
var clips = controller.animationClips;
|
||||
|
||||
foreach (var clip in clips)
|
||||
{
|
||||
if (clip.name.Contains("Up")) // Thay bằng tên thực tế của clip U
|
||||
{
|
||||
animationUClip = clip;
|
||||
}
|
||||
else if (clip.name.Contains("Down")) // Thay bằng tên thực tế của clip D
|
||||
{
|
||||
animationDClip = clip;
|
||||
}
|
||||
}
|
||||
|
||||
if (animationUClip == null || animationDClip == null)
|
||||
{
|
||||
Debug.LogWarning("Không tìm thấy animation clip cho U hoặc D. Kiểm tra tên clip hoặc AnimatorController.");
|
||||
}
|
||||
}
|
||||
|
||||
// Kích hoạt animation "Down"
|
||||
public void OnKey1()
|
||||
{
|
||||
DetachPallet(); // Tách pallet khỏi fork
|
||||
animator.SetBool("U", false);
|
||||
animator.SetBool("D", true);
|
||||
currentActionType = "drop";
|
||||
}
|
||||
|
||||
// Kích hoạt animation "Up"
|
||||
public void OnKey2()
|
||||
{
|
||||
AttachPalletToFork(); // Gắn pallet vào fork
|
||||
animator.SetBool("U", true);
|
||||
animator.SetBool("D", false);
|
||||
currentActionType = "pick";
|
||||
}
|
||||
|
||||
// Phương thức gắn pallet vào fork
|
||||
public void AttachPalletToFork()
|
||||
{
|
||||
if (pallet != null && fork != null)
|
||||
{
|
||||
pallet.SetParent(fork);
|
||||
Debug.Log("Pallet đã được gắn vào fork.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Pallet hoặc fork không được gán trong Inspector.");
|
||||
}
|
||||
}
|
||||
|
||||
// Phương thức tách pallet khỏi fork
|
||||
public void DetachPallet()
|
||||
{
|
||||
if (pallet != null)
|
||||
{
|
||||
pallet.SetParent(null);
|
||||
Debug.Log("Pallet đã được tách khỏi fork.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Pallet không được gán trong Inspector.");
|
||||
}
|
||||
}
|
||||
|
||||
public float GetAnimationDuration(string actionType)
|
||||
{
|
||||
switch (actionType)
|
||||
{
|
||||
case "pick":
|
||||
return AnimationUDuration;
|
||||
case "drop":
|
||||
return AnimationDDuration;
|
||||
default:
|
||||
Debug.LogWarning($"ActionType {actionType} không được hỗ trợ.");
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Gọi bởi Animation Event khi animation bắt đầu
|
||||
public void OnAnimationStart()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentActionType))
|
||||
{
|
||||
Debug.Log($"Animation bắt đầu: {currentActionType}");
|
||||
OnAnimationStateChanged?.Invoke(currentActionType, "RUNNING");
|
||||
}
|
||||
}
|
||||
|
||||
// Gọi bởi Animation Event khi animation kết thúc
|
||||
public void OnAnimationEnd()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentActionType))
|
||||
{
|
||||
Debug.Log($"Animation kết thúc: {currentActionType}");
|
||||
OnAnimationStateChanged?.Invoke(currentActionType, "FINISHED");
|
||||
currentActionType = null; // Reset sau khi hoàn tất
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripting/Khac/AnimationControllerAPM.cs.meta
Normal file
2
Assets/Scripting/Khac/AnimationControllerAPM.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17549d08c68a63646b3568dd46f952a2
|
||||
54
Assets/Scripting/Khac/CheckAnimation.cs
Normal file
54
Assets/Scripting/Khac/CheckAnimation.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CheckAnimation : MonoBehaviour
|
||||
{
|
||||
// Hàm kiểm tra xem GameObject có animation hay không
|
||||
public bool HasAnimation(GameObject targetObject)
|
||||
{
|
||||
// Kiểm tra Animator Component (Mecanim)
|
||||
Animator animator = targetObject.GetComponent<Animator>();
|
||||
if (animator != null)
|
||||
{
|
||||
// Kiểm tra xem Animator có AnimatorController được gắn không
|
||||
if (animator.runtimeAnimatorController != null)
|
||||
{
|
||||
Debug.Log($"{targetObject.name} có Animator với AnimatorController.");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"{targetObject.name} có Animator nhưng không có AnimatorController.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Kiểm tra Animation Component (Legacy)
|
||||
Animation animation = targetObject.GetComponent<Animation>();
|
||||
if (animation != null)
|
||||
{
|
||||
// Kiểm tra xem Animation có AnimationClip được gắn không
|
||||
if (animation.clip != null || animation.GetClipCount() > 0)
|
||||
{
|
||||
Debug.Log($"{targetObject.name} có Animation với AnimationClip.");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"{targetObject.name} có Animation nhưng không có AnimationClip.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Nếu không có Animator hoặc Animation
|
||||
Debug.Log($"{targetObject.name} không có Animator hoặc Animation.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ví dụ sử dụng
|
||||
void Start()
|
||||
{
|
||||
// Kiểm tra chính GameObject mà script này được gắn vào
|
||||
bool hasAnim = HasAnimation(gameObject);
|
||||
Debug.Log($"Kết quả kiểm tra: {hasAnim}");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripting/Khac/CheckAnimation.cs.meta
Normal file
2
Assets/Scripting/Khac/CheckAnimation.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c236d6a427a698349af4be427f1bc259
|
||||
39
Assets/Scripting/Khac/DisplayPosition.cs
Normal file
39
Assets/Scripting/Khac/DisplayPosition.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
using TMPro; // Sử dụng TextMeshPro
|
||||
|
||||
public class DisplayPosition : MonoBehaviour
|
||||
{
|
||||
// Gán TextMeshProUGUI trong Inspector
|
||||
public TextMeshProUGUI positionText;
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Lấy vị trí của đối tượng
|
||||
Vector3 position = transform.position;
|
||||
float displayEulerY = NormalizeEulerAngle(-transform.eulerAngles.y); // Giữ nguyên hiển thị
|
||||
float theta = NormalizeAngle(2 * Mathf.PI - transform.eulerAngles.y * Mathf.Deg2Rad); // Công thức mới
|
||||
|
||||
// Hiển thị tọa độ x và z
|
||||
string positionInfo = $"X: {position.x:F2} \n" +
|
||||
$"Y: {position.z:F2} \n" +
|
||||
$"Theta: {displayEulerY:F2}" ;
|
||||
|
||||
// Cập nhật TextMeshProUGUI
|
||||
if (positionText != null)
|
||||
{
|
||||
positionText.text = positionInfo;
|
||||
}
|
||||
}
|
||||
public float NormalizeEulerAngle(float angleDeg)
|
||||
{
|
||||
while (angleDeg > 180f) angleDeg -= 360f;
|
||||
while (angleDeg < -180f) angleDeg += 360f;
|
||||
return angleDeg;
|
||||
}
|
||||
public float NormalizeAngle(float angleRad)
|
||||
{
|
||||
while (angleRad > Mathf.PI) angleRad -= 2 * Mathf.PI;
|
||||
while (angleRad < -Mathf.PI) angleRad += 2 * Mathf.PI;
|
||||
return angleRad;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripting/Khac/DisplayPosition.cs.meta
Normal file
2
Assets/Scripting/Khac/DisplayPosition.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73fb5f1bf8e3fd341baf281a45510492
|
||||
34
Assets/Scripting/Khac/SceneSetup.cs
Normal file
34
Assets/Scripting/Khac/SceneSetup.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SceneSetup : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform groundPlanePrefab; // Transform của Prefab Plane
|
||||
[SerializeField] private Transform amrPrefab; // Transform của Prefab AMR
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Khởi tạo Plane
|
||||
if (groundPlanePrefab != null)
|
||||
{
|
||||
GameObject plane = Instantiate(groundPlanePrefab.gameObject, groundPlanePrefab.position, groundPlanePrefab.rotation);
|
||||
plane.name = "GroundPlane";
|
||||
Debug.Log($"Đã khởi tạo GroundPlane Prefab tại position: {groundPlanePrefab.position}, rotation: {groundPlanePrefab.rotation.eulerAngles}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("GroundPlane Prefab (Transform) chưa được gán trong Inspector!");
|
||||
}
|
||||
|
||||
// Khởi tạo AMR
|
||||
if (amrPrefab != null)
|
||||
{
|
||||
GameObject amr = Instantiate(amrPrefab.gameObject, amrPrefab.position, amrPrefab.rotation);
|
||||
amr.name = "AMR";
|
||||
Debug.Log($"Đã khởi tạo AMR Prefab tại position: {amrPrefab.position}, rotation: {amrPrefab.rotation.eulerAngles}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("AMR Prefab (Transform) chưa được gán trong Inspector!");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripting/Khac/SceneSetup.cs.meta
Normal file
2
Assets/Scripting/Khac/SceneSetup.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c93a95dd176e6a644b2443303a886c89
|
||||
Reference in New Issue
Block a user