добавлены модели анимации персонажа
This commit is contained in:
1 parent
11d57bec19
commit
202a894140
824 files changed
+985212
-40
No files matched your search
@@ -0,0 +1,210 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
|
||||
public static class CharacterSetup
|
||||
{
|
||||
private const string FbxPath = "Assets/mainhero/balthazar-rigged-animated/source/Balthazar walking/Balthazar walking.Fbx";
|
||||
private const string ControllerPath = "Assets/mainhero/PlayerAnimator.controller";
|
||||
private const string MaterialsFolder = "Assets/mainhero/Materials";
|
||||
|
||||
[MenuItem("Tools/Setup Main Hero On Player")]
|
||||
public static string ExecuteSetup()
|
||||
{
|
||||
var fbxPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(FbxPath);
|
||||
if (fbxPrefab == null)
|
||||
{
|
||||
Debug.LogError($"[CharacterSetup] FBX model not found at {FbxPath}");
|
||||
return "FBX model not found!";
|
||||
}
|
||||
|
||||
var controller = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(ControllerPath);
|
||||
if (controller == null)
|
||||
{
|
||||
Debug.LogError($"[CharacterSetup] AnimatorController not found at {ControllerPath}");
|
||||
return "AnimatorController not found!";
|
||||
}
|
||||
|
||||
// 1. Находим игрока на сцене (Capsule или Player)
|
||||
GameObject player = GameObject.Find("Capsule");
|
||||
if (player == null)
|
||||
{
|
||||
player = GameObject.Find("Player");
|
||||
}
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
// Попробуем найти через PlayerMovement
|
||||
var pmFound = Object.FindAnyObjectByType<PlayerMovement>();
|
||||
if (pmFound != null) player = pmFound.gameObject;
|
||||
}
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return "Player / Capsule GameObject not found in scene!";
|
||||
}
|
||||
|
||||
Undo.RegisterFullObjectHierarchyUndo(player, "Setup Main Hero Character");
|
||||
|
||||
// 2. Переименовываем в Player для порядка
|
||||
player.name = "Player";
|
||||
|
||||
// 3. Отключаем визуальный меш капсулы
|
||||
var meshRenderer = player.GetComponent<MeshRenderer>();
|
||||
if (meshRenderer != null)
|
||||
{
|
||||
meshRenderer.enabled = false;
|
||||
}
|
||||
|
||||
// Отключаем лишний CapsuleCollider, так как CharacterController сам является коллайдером
|
||||
var capCol = player.GetComponent<CapsuleCollider>();
|
||||
if (capCol != null)
|
||||
{
|
||||
capCol.enabled = false;
|
||||
}
|
||||
|
||||
// 4. Проверяем наличие дочерней модели, удаляем старую при повторном запуске
|
||||
Transform oldModel = player.transform.Find("Model");
|
||||
if (oldModel != null)
|
||||
{
|
||||
Object.DestroyImmediate(oldModel.gameObject);
|
||||
}
|
||||
|
||||
// 5. Создаем экземпляр модели из FBX как дочерний объект
|
||||
GameObject modelGo = (GameObject)PrefabUtility.InstantiatePrefab(fbxPrefab, player.transform);
|
||||
modelGo.name = "Model";
|
||||
modelGo.transform.localPosition = new Vector3(0f, -1f, 0f); // Смещаем вниз к основанию CharacterController
|
||||
modelGo.transform.localRotation = Quaternion.identity;
|
||||
modelGo.transform.localScale = Vector3.one;
|
||||
|
||||
// 6. Подгружаем материалы из папки Materials
|
||||
var matDict = new Dictionary<string, Material>();
|
||||
string[] matGuids = AssetDatabase.FindAssets("t:Material", new[] { MaterialsFolder });
|
||||
foreach (var guid in matGuids)
|
||||
{
|
||||
string p = AssetDatabase.GUIDToAssetPath(guid);
|
||||
var m = AssetDatabase.LoadAssetAtPath<Material>(p);
|
||||
if (m != null)
|
||||
{
|
||||
matDict[m.name] = m;
|
||||
}
|
||||
}
|
||||
|
||||
// Назначаем материалы всем рендерам модели
|
||||
var renderers = modelGo.GetComponentsInChildren<SkinnedMeshRenderer>(true);
|
||||
foreach (var r in renderers)
|
||||
{
|
||||
var mats = r.sharedMaterials;
|
||||
for (int i = 0; i < mats.Length; i++)
|
||||
{
|
||||
if (mats[i] != null && matDict.TryGetValue(mats[i].name, out var foundMat))
|
||||
{
|
||||
mats[i] = foundMat;
|
||||
}
|
||||
}
|
||||
r.sharedMaterials = mats;
|
||||
}
|
||||
|
||||
// 7. Настраиваем Animator
|
||||
var animator = modelGo.GetComponent<Animator>();
|
||||
if (animator == null)
|
||||
{
|
||||
animator = modelGo.AddComponent<Animator>();
|
||||
}
|
||||
|
||||
// Подгружаем Humanoid Avatar модели
|
||||
var subAssets = AssetDatabase.LoadAllAssetsAtPath(FbxPath);
|
||||
foreach (var a in subAssets)
|
||||
{
|
||||
if (a is Avatar av)
|
||||
{
|
||||
animator.avatar = av;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
animator.runtimeAnimatorController = controller;
|
||||
animator.applyRootMotion = false; // Движением управляет CharacterController
|
||||
|
||||
// 8. Привязываем аниматор к PlayerMovement, PlayerAttack и PlayerGun
|
||||
var pm = player.GetComponent<PlayerMovement>();
|
||||
if (pm != null)
|
||||
{
|
||||
pm.animator = animator;
|
||||
EditorUtility.SetDirty(pm);
|
||||
}
|
||||
|
||||
var pa = player.GetComponent<PlayerAttack>();
|
||||
if (pa != null)
|
||||
{
|
||||
pa.animator = animator;
|
||||
EditorUtility.SetDirty(pa);
|
||||
}
|
||||
|
||||
var pg = player.GetComponent<PlayerGun>();
|
||||
if (pg != null)
|
||||
{
|
||||
pg.playerAnimator = animator;
|
||||
EditorUtility.SetDirty(pg);
|
||||
}
|
||||
|
||||
// 9. Проверяем CameraFollow на главной камере
|
||||
if (Camera.main != null)
|
||||
{
|
||||
var camFollow = Camera.main.GetComponent<CameraFollow>();
|
||||
if (camFollow != null)
|
||||
{
|
||||
camFollow.target = player.transform;
|
||||
EditorUtility.SetDirty(camFollow);
|
||||
}
|
||||
}
|
||||
|
||||
// 10. Находим кость правой руки для оружия
|
||||
Transform rHand = animator.GetBoneTransform(HumanBodyBones.RightHand);
|
||||
Transform gunHolder = rHand != null ? rHand.Find("GunHolder") : null;
|
||||
if (gunHolder == null) gunHolder = player.transform.Find("GunHolder");
|
||||
|
||||
if (gunHolder != null && rHand != null && gunHolder.parent != rHand)
|
||||
{
|
||||
gunHolder.SetParent(rHand, false);
|
||||
gunHolder.localPosition = new Vector3(0.08f, 0.04f, -0.02f);
|
||||
gunHolder.localRotation = Quaternion.Euler(279.58f, 351.41f, 180.18f);
|
||||
}
|
||||
|
||||
if (gunHolder != null && gunHolder.Find("M1911 Handgun_Black") == null)
|
||||
{
|
||||
for (int i = gunHolder.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Object.DestroyImmediate(gunHolder.GetChild(i).gameObject);
|
||||
}
|
||||
var gunPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Nokobot/Modern Guns - Handgun/_Prefabs/Handgun Black/M1911 Handgun_Black.prefab");
|
||||
if (gunPrefab != null)
|
||||
{
|
||||
var gunInst = (GameObject)PrefabUtility.InstantiatePrefab(gunPrefab, gunHolder);
|
||||
gunInst.name = "M1911 Handgun_Black";
|
||||
gunInst.transform.localPosition = Vector3.zero;
|
||||
gunInst.transform.localRotation = Quaternion.identity;
|
||||
|
||||
if (pg != null)
|
||||
{
|
||||
Transform barrel = gunInst.transform.Find("Barrel_Location");
|
||||
Transform modelM = gunInst.transform.Find("M1911 Handgun_Model");
|
||||
pg.muzzlePoint = barrel;
|
||||
pg.gunAnimator = modelM != null ? modelM.GetComponent<Animator>() : null;
|
||||
pg.casingExitPoint = modelM != null ? modelM.Find("CasingExit") : null;
|
||||
pg.gunModelTransform = gunHolder;
|
||||
pg.muzzleFlashPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Nokobot/Modern Guns - Handgun/Effects/MuzzleFlash.prefab");
|
||||
pg.casingPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Nokobot/Modern Guns - Handgun/_Prefabs/45ACP Bullet_Casing.prefab");
|
||||
EditorUtility.SetDirty(pg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EditorSceneManager.MarkSceneDirty(player.scene);
|
||||
EditorSceneManager.SaveScene(player.scene);
|
||||
|
||||
Debug.Log("[CharacterSetup] Main hero character successfully configured on Player!");
|
||||
return "SUCCESS: Main hero character successfully configured on Player!";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbdd83ef8b5eff949bf0965fc18de69c
|
||||
@@ -0,0 +1,211 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Animations;
|
||||
|
||||
public static class SetupPlayerAnimatorLayers
|
||||
{
|
||||
private const string ControllerPath = "Assets/mainhero/PlayerAnimator.controller";
|
||||
private const string UpperMaskPath = "Assets/Kevin Iglesias/Human Animations/Models/Avatar Masks/Human Body Upper Mask.mask";
|
||||
private const string IdleFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Idles/HumanM@Idle01.fbx";
|
||||
private const string WalkFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Movement/Walk/HumanM@Walk01_Forward.fbx";
|
||||
private const string SprintFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Movement/Sprint/HumanM@Sprint01_Forward.fbx";
|
||||
private const string AttackFbxPath = "Assets/Brawler Animations/Animations/Atk_P_1.fbx";
|
||||
private const string GunAimFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Combat/Gun/HumanM@Gun_Aim01.fbx";
|
||||
private const string GunShootFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Combat/Gun/HumanM@Gun_Aim01_Shoot01.fbx";
|
||||
private const string GunReloadFbxPath = "Assets/Kevin Iglesias/Human Animations/Animations/Male/Combat/Gun/HumanM@Gun_Reload01.fbx";
|
||||
|
||||
public static AnimationClip FindClip(string fbxPath)
|
||||
{
|
||||
var all = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
|
||||
foreach (var a in all)
|
||||
{
|
||||
if (a is AnimationClip c && !c.name.StartsWith("__preview"))
|
||||
{
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
[MenuItem("Tools/Rebuild Player Animator Controller")]
|
||||
public static string Rebuild()
|
||||
{
|
||||
var controller = AssetDatabase.LoadAssetAtPath<AnimatorController>(ControllerPath);
|
||||
if (controller == null)
|
||||
{
|
||||
controller = AnimatorController.CreateAnimatorControllerAtPath(ControllerPath);
|
||||
}
|
||||
|
||||
// 1. Параметры
|
||||
AddOrUpdateParam(controller, "Speed", AnimatorControllerParameterType.Float, 0f);
|
||||
AddOrUpdateParam(controller, "IsMoving", AnimatorControllerParameterType.Bool, false);
|
||||
AddOrUpdateParam(controller, "Attack", AnimatorControllerParameterType.Trigger);
|
||||
AddOrUpdateParam(controller, "HasGun", AnimatorControllerParameterType.Bool, false);
|
||||
AddOrUpdateParam(controller, "Shoot", AnimatorControllerParameterType.Trigger);
|
||||
AddOrUpdateParam(controller, "Reload", AnimatorControllerParameterType.Trigger);
|
||||
|
||||
// 2. Base Layer
|
||||
var baseSm = controller.layers[0].stateMachine;
|
||||
// Очищаем существующие состояния
|
||||
for (int i = baseSm.states.Length - 1; i >= 0; i--)
|
||||
{
|
||||
baseSm.RemoveState(baseSm.states[i].state);
|
||||
}
|
||||
for (int i = baseSm.anyStateTransitions.Length - 1; i >= 0; i--)
|
||||
{
|
||||
baseSm.RemoveAnyStateTransition(baseSm.anyStateTransitions[i]);
|
||||
}
|
||||
|
||||
// Создаем BlendTree Locomotion
|
||||
var locomotionState = baseSm.AddState("Locomotion", new Vector3(250, 50, 0));
|
||||
var blendTree = new BlendTree
|
||||
{
|
||||
name = "Locomotion",
|
||||
blendType = BlendTreeType.Simple1D,
|
||||
blendParameter = "Speed",
|
||||
useAutomaticThresholds = false
|
||||
};
|
||||
AssetDatabase.AddObjectToAsset(blendTree, controller);
|
||||
|
||||
var idleClip = FindClip(IdleFbxPath);
|
||||
var walkClip = FindClip(WalkFbxPath);
|
||||
var sprintClip = FindClip(SprintFbxPath);
|
||||
|
||||
blendTree.AddChild(idleClip, 0f);
|
||||
blendTree.AddChild(walkClip, 1.5f);
|
||||
blendTree.AddChild(walkClip, 3.0f);
|
||||
blendTree.AddChild(sprintClip, 6.0f);
|
||||
|
||||
locomotionState.motion = blendTree;
|
||||
baseSm.defaultState = locomotionState;
|
||||
|
||||
// Создаем Melee Attack State
|
||||
var attackClip = FindClip(AttackFbxPath);
|
||||
if (attackClip != null)
|
||||
{
|
||||
var attackState = baseSm.AddState("Attack", new Vector3(250, 150, 0));
|
||||
attackState.motion = attackClip;
|
||||
attackState.speed = 1.15f;
|
||||
|
||||
var toAttack = baseSm.AddAnyStateTransition(attackState);
|
||||
toAttack.AddCondition(AnimatorConditionMode.If, 0f, "Attack");
|
||||
toAttack.duration = 0.08f;
|
||||
toAttack.canTransitionToSelf = false;
|
||||
|
||||
var fromAttack = attackState.AddTransition(locomotionState);
|
||||
fromAttack.hasExitTime = true;
|
||||
fromAttack.exitTime = 0.85f;
|
||||
fromAttack.duration = 0.15f;
|
||||
}
|
||||
|
||||
// 3. UpperBody Layer (для удержания оружия, выстрела, перезарядки)
|
||||
var upperMask = AssetDatabase.LoadAssetAtPath<AvatarMask>(UpperMaskPath);
|
||||
if (upperMask == null)
|
||||
{
|
||||
Debug.LogError("[SetupPlayerAnimatorLayers] Upper body mask not found!");
|
||||
}
|
||||
|
||||
// Удаляем слой UpperBody если уже существовал
|
||||
for (int i = controller.layers.Length - 1; i >= 1; i--)
|
||||
{
|
||||
controller.RemoveLayer(i);
|
||||
}
|
||||
|
||||
controller.AddLayer("UpperBody Layer");
|
||||
var layers = controller.layers;
|
||||
var upperLayer = layers[1];
|
||||
upperLayer.defaultWeight = 0f; // Управляется PlayerGun скриптом
|
||||
upperLayer.blendingMode = AnimatorLayerBlendingMode.Override;
|
||||
upperLayer.avatarMask = upperMask;
|
||||
|
||||
var upperSm = upperLayer.stateMachine;
|
||||
var emptyState = upperSm.AddState("Empty", new Vector3(250, 50, 0));
|
||||
emptyState.motion = null;
|
||||
upperSm.defaultState = emptyState;
|
||||
|
||||
var gunAimClip = FindClip(GunAimFbxPath);
|
||||
var gunShootClip = FindClip(GunShootFbxPath);
|
||||
var gunReloadClip = FindClip(GunReloadFbxPath);
|
||||
|
||||
if (gunAimClip != null)
|
||||
{
|
||||
var gunAimState = upperSm.AddState("Gun_Aim", new Vector3(250, 150, 0));
|
||||
gunAimState.motion = gunAimClip;
|
||||
|
||||
// Empty <-> Gun_Aim
|
||||
var toAim = emptyState.AddTransition(gunAimState);
|
||||
toAim.AddCondition(AnimatorConditionMode.If, 0f, "HasGun");
|
||||
toAim.duration = 0.15f;
|
||||
toAim.hasExitTime = false;
|
||||
|
||||
var toEmpty = gunAimState.AddTransition(emptyState);
|
||||
toEmpty.AddCondition(AnimatorConditionMode.IfNot, 0f, "HasGun");
|
||||
toEmpty.duration = 0.15f;
|
||||
toEmpty.hasExitTime = false;
|
||||
|
||||
// Gun_Aim <-> Shoot
|
||||
if (gunShootClip != null)
|
||||
{
|
||||
var shootState = upperSm.AddState("Shoot", new Vector3(500, 100, 0));
|
||||
shootState.motion = gunShootClip;
|
||||
|
||||
var toShoot = gunAimState.AddTransition(shootState);
|
||||
toShoot.AddCondition(AnimatorConditionMode.If, 0f, "Shoot");
|
||||
toShoot.duration = 0.05f;
|
||||
toShoot.hasExitTime = false;
|
||||
|
||||
var fromShoot = shootState.AddTransition(gunAimState);
|
||||
fromShoot.hasExitTime = true;
|
||||
fromShoot.exitTime = 0.8f;
|
||||
fromShoot.duration = 0.1f;
|
||||
}
|
||||
|
||||
// Gun_Aim <-> Reload
|
||||
if (gunReloadClip != null)
|
||||
{
|
||||
var reloadState = upperSm.AddState("Reload", new Vector3(500, 200, 0));
|
||||
reloadState.motion = gunReloadClip;
|
||||
|
||||
var toReload = gunAimState.AddTransition(reloadState);
|
||||
toReload.AddCondition(AnimatorConditionMode.If, 0f, "Reload");
|
||||
toReload.duration = 0.1f;
|
||||
toReload.hasExitTime = false;
|
||||
|
||||
var fromReload = reloadState.AddTransition(gunAimState);
|
||||
fromReload.hasExitTime = true;
|
||||
fromReload.exitTime = 0.9f;
|
||||
fromReload.duration = 0.15f;
|
||||
}
|
||||
}
|
||||
|
||||
controller.layers = layers;
|
||||
|
||||
EditorUtility.SetDirty(controller);
|
||||
AssetDatabase.SaveAssets();
|
||||
|
||||
Debug.Log("[SetupPlayerAnimatorLayers] Controller successfully rebuilt with Base and UpperBody layers!");
|
||||
return "SUCCESS";
|
||||
}
|
||||
|
||||
private static void AddOrUpdateParam(AnimatorController ctrl, string name, AnimatorControllerParameterType type, object defVal = null)
|
||||
{
|
||||
foreach (var p in ctrl.parameters)
|
||||
{
|
||||
if (p.name == name) return;
|
||||
}
|
||||
ctrl.AddParameter(name, type);
|
||||
if (defVal != null)
|
||||
{
|
||||
for (int i = 0; i < ctrl.parameters.Length; i++)
|
||||
{
|
||||
if (ctrl.parameters[i].name == name)
|
||||
{
|
||||
if (defVal is float f) ctrl.parameters[i].defaultFloat = f;
|
||||
else if (defVal is bool b) ctrl.parameters[i].defaultBool = b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 99fe9b005a666e743b3b23d5bd79fd0d
|
||||
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
public static class TestPlayerPose
|
||||
{
|
||||
public static void Sample()
|
||||
{
|
||||
var player = GameObject.Find("Player");
|
||||
if (player == null) player = GameObject.Find("Capsule");
|
||||
if (player == null) { Debug.LogError("Player not found"); return; }
|
||||
|
||||
var anim = player.GetComponentInChildren<Animator>();
|
||||
if (anim == null) { Debug.LogError("Animator not found"); return; }
|
||||
|
||||
Debug.Log("== PLAYER ANIMATION SAMPLING ==");
|
||||
Debug.Log("Speed param: " + anim.GetFloat("Speed"));
|
||||
Debug.Log("IsMoving param: " + anim.GetBool("IsMoving"));
|
||||
|
||||
var stateInfo = anim.GetCurrentAnimatorStateInfo(0);
|
||||
Debug.Log("Current State Hash: " + stateInfo.shortNameHash + ", normalizedTime: " + stateInfo.normalizedTime);
|
||||
Debug.Log("Is Locomotion state: " + stateInfo.IsName("Locomotion"));
|
||||
|
||||
var leftFoot = anim.GetBoneTransform(HumanBodyBones.LeftFoot);
|
||||
var rightFoot = anim.GetBoneTransform(HumanBodyBones.RightFoot);
|
||||
var leftHand = anim.GetBoneTransform(HumanBodyBones.LeftHand);
|
||||
var rightHand = anim.GetBoneTransform(HumanBodyBones.RightHand);
|
||||
|
||||
if (leftFoot != null && rightFoot != null)
|
||||
{
|
||||
Debug.Log("LeftFoot pos: " + leftFoot.position + ", RightFoot pos: " + rightFoot.position + ", Distance Z: " + Mathf.Abs(leftFoot.position.z - rightFoot.position.z));
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("LeftFoot or RightFoot bone is null! Humanoid bones not bound?");
|
||||
}
|
||||
|
||||
if (leftHand != null && rightHand != null)
|
||||
{
|
||||
Debug.Log("LeftHand pos: " + leftHand.position + ", RightHand pos: " + rightHand.position);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Hands bone is null!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f365902da7acb10498d0a3c32845b5e2
|
||||
Reference in new issue
Block a user