Forum begins after the advertisement:


[Part7]Issues with seed planting mechanics and time control during sprouting

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part7]Issues with seed planting mechanics and time control during sprouting

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #19485
    Revati Iyer
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Hi, how do you fast forward the time while checking the sprouting? Also, thank you so much for this tutorial.

    I’m facing another problem: I’m not able to plant seeds smoothly. I have to keep clicking on the inventory bag repeatedly to either plant, water, or use any other tool. This makes the process very inefficient.

    I downloaded the zip file, and I’m using Unity version 2019.4.41f2. Can you help me fix this issue?

    Following code is for land:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Land : MonoBehaviour, ITimeTracker
    {
        public enum LandStatus
        {
            Soil, Farmland, Watered
        }
    
        public LandStatus landStatus;
    
        public Material soilMat, farmlandMat, wateredMat;
        new Renderer renderer;
    
        //The selection gameobject to enable when the player is selecting the land
        public GameObject select;
    
        //Cache the time the land was watered 
        GameTimestamp timeWatered;
    
        [Header("Crops")]
        //The crop prefab to instantiate
        public GameObject cropPrefab;
    
        //The crop currently planted on the land
        CropBehaviour cropPlanted = null;
    
        // Start is called before the first frame update
        void Start()
        {
            //Get the renderer component
            renderer = GetComponent<Renderer>();
    
            //Set the land to soil by default
            SwitchLandStatus(LandStatus.Soil);
    
            //Deselect the land by default
            Select(false);
    
            //Add this to TimeManager's Listener list
            TimeManager.Instance.RegisterTracker(this);
        }
    
        public void SwitchLandStatus(LandStatus statusToSwitch)
        {
            //Set land status accordingly
            landStatus = statusToSwitch;
    
            Material materialToSwitch = soilMat; 
    
            //Decide what material to switch to
            switch (statusToSwitch)
            {
                case LandStatus.Soil:
                    //Switch to the soil material
                    materialToSwitch = soilMat;
                    break;
                case LandStatus.Farmland:
                    //Switch to farmland material 
                    materialToSwitch = farmlandMat;
                    break;
    
                case LandStatus.Watered:
                    //Switch to watered material
                    materialToSwitch = wateredMat;
    
                    //Cache the time it was watered
                    timeWatered = TimeManager.Instance.GetGameTimestamp(); 
                    break; 
    
            }
    
            //Get the renderer to apply the changes
            renderer.material = materialToSwitch; 
        }
    
        public void Select(bool toggle)
        {
            select.SetActive(toggle);
        }
    
        //When the player presses the interact button while selecting this land
        public void Interact()
        {
            //Check the player's tool slot
            ItemData toolSlot = InventoryManager.Instance.equippedTool;
    
            //If there's nothing equipped, return
            if (toolSlot == null)
            {
                return; 
            }
    
            //Try casting the itemdata in the toolslot as EquipmentData
            EquipmentData equipmentTool = toolSlot as EquipmentData; 
    
            //Check if it is of type EquipmentData 
            if(equipmentTool != null)
            {
                //Get the tool type
                EquipmentData.ToolType toolType = equipmentTool.toolType;
    
                switch (toolType)
                {
                    case EquipmentData.ToolType.Hoe:
                        SwitchLandStatus(LandStatus.Farmland);
                        break;
                    case EquipmentData.ToolType.WateringCan:
                        SwitchLandStatus(LandStatus.Watered);
                        break;
                }
    
                //We don't need to check for seeds if we have already confirmed the tool to be an equipment
                return; 
            }
    
            //Try casting the itemdata in the toolslot as SeedData
            SeedData seedTool = toolSlot as SeedData; 
    
            ///Conditions for the player to be able to plant a seed
            ///1: He is holding a tool of type SeedData
            ///2: The Land State must be either watered or farmland
            ///3. There isn't already a crop that has been planted
            if(seedTool != null && landStatus != LandStatus.Soil && cropPlanted == null)
            {
                //Instantiate the crop object parented to the land
                GameObject cropObject = Instantiate(cropPrefab, transform);
                //Move the crop object to the top of the land gameobject
                cropObject.transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    
                //Access the CropBehaviour of the crop we're going to plant
                cropPlanted = cropObject.GetComponent<CropBehaviour>();
                //Plant it with the seed's information
                cropPlanted.Plant(seedTool);
    
            }
        }
    
        public void ClockUpdate(GameTimestamp timestamp)
        {
            //Checked if 24 hours has passed since last watered
            if(landStatus == LandStatus.Watered)
            {
                //Hours since the land was watered
                int hoursElapsed = GameTimestamp.CompareTimestamps(timeWatered, timestamp);
                Debug.Log(hoursElapsed + " hours since this was watered");
    
                //Grow the planted crop, if any
                if(cropPlanted != null)
                {
                    cropPlanted.Grow();
                }
    
                if(hoursElapsed > 24)
                {
                    //Dry up (Switch back to farmland)
                    SwitchLandStatus(LandStatus.Farmland);
                }
            }
        }
    }

    Following code is for CROP BEHAVIOUR:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CropBehaviour : MonoBehaviour
    {
        //Information on what the crop will grow into 
        SeedData seedToGrow;
    
        [Header("Stages of Life")]
        public GameObject seed;
        private GameObject seedling;
        private GameObject harvestable;
    
        //The growth points of the crop
        int growth;
        //How many growth points it takes before it becomes harvestable
        int maxGrowth; 
    
        public enum CropState
        {
            Seed, Seedling, Harvestable
        }
        //The current stage in the crop's growth
        public CropState cropState;
    
        //Initialisation for the crop GameObject
        //Called when the player plants a seed
        public void Plant(SeedData seedToGrow)
        {
            //Save the seed information
            this.seedToGrow = seedToGrow;
    
            //Instantiate the seedling and harvestable GameObjects
            seedling = Instantiate(seedToGrow.seedling, transform);
    
            //Access the crop item data
            ItemData cropToYield = seedToGrow.cropToYield;
    
            //Instantiate the harvestable crop
            harvestable = Instantiate(cropToYield.gameModel, transform);
    
            //Convert Days To Grow into hours
            int hoursToGrow = GameTimestamp.DaysToHours(seedToGrow.daysToGrow);
            //Convert it to minutes
            maxGrowth = GameTimestamp.HoursToMinutes(hoursToGrow); 
    
            //Set the initial state to Seed
            SwitchState(CropState.Seed); 
    
        }
    
        //The crop will grow when watered
        public void Grow()
        {
            //Increase the growth point by 1
            growth++;
    
            //The seed will sprout into a seedling when the growth is at 50%
            if(growth >= maxGrowth / 2 && cropState == CropState.Seed)
            {
                SwitchState(CropState.Seedling); 
            }
    
            //Grow from seedling to harvestable
            if(growth >= maxGrowth && cropState == CropState.Seedling)
            {
                SwitchState(CropState.Harvestable);
            }
        }
    
        //Function to handle the state changes 
        void SwitchState(CropState stateToSwitch)
        {
            //Reset everything and set all GameObjects to inactive
            seed.SetActive(false);
            seedling.SetActive(false);
            harvestable.SetActive(false);
    
            switch (stateToSwitch)
            {
                case CropState.Seed:
                    //Enable the Seed GameObject
                    seed.SetActive(true);
                    break;
                case CropState.Seedling:
                    //Enable the Seedling GameObject
                    seedling.SetActive(true);
                    break;
                case CropState.Harvestable:
                    //Enable the Harvestable GameObject
                    harvestable.SetActive(true);
                    //Unparent it to the crop
                    harvestable.transform.parent = null;
    
                    Destroy(gameObject); 
                    break;
            }
    
            //Set the current crop state to the state we're switching to
            cropState = stateToSwitch; 
        }
    }
    #19502
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    Hi, you hold down the right square bracker (“]”) to fast forward the time.

    In regards to the inventory design, just continue to follow the series, the improvements to the inventory system will be made.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: