From 2a29e5413b54042e470531cff997d54f7eac5409 Mon Sep 17 00:00:00 2001 From: d-hain Date: Thu, 16 Jun 2022 15:25:54 +0200 Subject: [PATCH] Animals can now move and stop and be animated to move cow now has idle and run animation --- Assets/Resources/Cow.prefab | 22 ++++++++++++++++++++++ Assets/Scripts/Animal.cs | 11 +++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Assets/Resources/Cow.prefab b/Assets/Resources/Cow.prefab index 151d87c..dfbb126 100644 --- a/Assets/Resources/Cow.prefab +++ b/Assets/Resources/Cow.prefab @@ -13,6 +13,7 @@ GameObject: - component: {fileID: 123187747779251107} - component: {fileID: 2510115116361252818} - component: {fileID: 4094795090309576525} + - component: {fileID: 3321960187907454328} m_Layer: 0 m_Name: Cow m_TagString: Untagged @@ -157,3 +158,24 @@ MonoBehaviour: producedItem: {fileID: 11400000, guid: 510712f2d23afc147979bcd564ef7943, type: 2} animalSprite: {fileID: 21300000, guid: e2bde586482642d44a270276574c0129, type: 3} movementSpeed: 1 + animator: {fileID: 3321960187907454328} +--- !u!95 &3321960187907454328 +Animator: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2510115116361252817} + m_Enabled: 1 + m_Avatar: {fileID: 0} + m_Controller: {fileID: 9100000, guid: 353868083b4723041bbf6c45effa9223, type: 2} + m_CullingMode: 0 + m_UpdateMode: 0 + m_ApplyRootMotion: 0 + m_LinearVelocityBlending: 0 + m_StabilizeFeet: 0 + m_WarningMessage: + m_HasTransformHierarchy: 1 + m_AllowConstantClipSamplingOptimization: 1 + m_KeepAnimatorControllerStateOnDisable: 0 diff --git a/Assets/Scripts/Animal.cs b/Assets/Scripts/Animal.cs index 4539f1e..aa5e207 100644 --- a/Assets/Scripts/Animal.cs +++ b/Assets/Scripts/Animal.cs @@ -6,14 +6,17 @@ using Random = UnityEngine.Random; public class Animal : MonoBehaviour { private Rigidbody2D _rigidbody; + private int _animMoveID; public Item producedItem; public Sprite animalSprite; public int movementSpeed; + public Animator animator; private void Start() { _rigidbody = gameObject.GetComponent(); animalSprite = gameObject.GetComponent().GetComponent(); + _animMoveID = Animator.StringToHash("moving"); // Move the Animal in any random direction every 1-5s InvokeRepeating(nameof(MoveInRandomDirection), 2f, Random.Range(1f, 5f)); @@ -31,15 +34,15 @@ public class Animal : MonoBehaviour { direction.Normalize(); _rigidbody.velocity = movementSpeed * direction; - StopAllCoroutines(); + animator.SetBool(_animMoveID, true); yield return new WaitForSeconds(randTime); - _rigidbody.velocity = new Vector2(0f, 0f); } + _rigidbody.velocity = new Vector2(0f, 0f); + animator.SetBool(_animMoveID, false); + } StartCoroutine(Move()); } - // TODO: Animations - private void OnMouseDown() { ActionInvoker.InvokeAction(gameObject, PlayerController.instance.SelectedItem); }