Click mouse and farm

plutonmania

Member
Good evening,
I have two new questions.
I hope you don't mind too much, I try to adapt to the best and understand everything, even if I have several months to understand. You don't have to answer me immediately and other people can probably answer for you!

Question 1 :
Is it possible to destroy an object only with certain weapons own. For example I based myself on the crate that destroys and trying to make a farm system on it. I assigned the same script to a tree.
But I would not like to be able to destroy it with my pistol or other tree of the same style. Just my sword or my ax for example.


Question 2 :
Is possible to remove the mouse in play mode except when I open the menu.
This one is walking everywhere when I turn the view and makes me click outside the view of the game.
 
1)
This will require a custom script.
From our previous discussion I believe you use the integration with the character controller (if so please move the thread to the character/inventory integration category)
If I'm mistaken please ignore all the things below and let me know.

There are two ways you could do it.
1_A) Have the weapon check what it is hitting and see if it can damage that object. This can be done through a custom "ImpactAction" or a custom "DamageProcessor".
You can learn about both of those here:


A good place to start is having a look at the "SimpleDamage" class to see how it works.


1_B) Another way is to have the object hit (example the tree) decide what gets to damage it. In that case you would create a custom script that listens to the "OnObjectImpact" event:
Code:
EventHandler.RegisterEvent<ImpactCallbackContext>(gameObject, "OnObjectImpact", OnObjectImpact);
(Check out the DebugObjectImpactReceiver class for an example)

From the ImpactCallbackContext you can get all the information you need about the imapct. It not only tells you if it is an item that hit but it can also be impacted by other things that are not items (explosions? enemy attacks? etc...)
So it has alot of flexibility.
If you go that route, then you wouldn't deal damage with SimpleDamage, but instead through this custom script.
You would still need "SimpleDamage" or "ImpactEvent" (or a custom ItemAction) to execute the "OnObjectImpact" event on the target.




No matter if you use 1_B or 1_A you'll be able to get the item category from the item and choose what damages what by checking if your itemcategory is part of a list of categories.



2)
Yes, this should have been explained in the integration documentation and video tutorials:
 
Hello, yes you are right,
I have the Character and Inventory system well, but I did not find how to move the thread to ---> Character/Inventory Integration Category.

And thank you for the responses 1A and 1b, (1B is better for me) I will try to docume me more suddenly, to succeed in doing it, being a novice in development.
 
I want you to move, my question in the right forum, so that people can help me by showing me an example or others, thank you and sorry for putting it in the wrong places.

As I have a lot of trouble understanding all your functions, I have your method 2. Duplicating the most of the debug I just remove the debug.
I did this :
test.PNG

But I don't think at all it's like that you have to do.

And for the input system or we desactive the Cursor, it's great!
But to reactivatedit I have to do the ESCHAP button (fortunately that I remove the fact of closing the menu with ECHAP to make it appear.
But wouldn't it be better than the mouse is deactivated when I am at stake and activates automatically when I open the shop with my action key and also for my inventory when I open it (which is "i" for the moment).
Having to do the ECHAP key each time in the menu for the reactivated will not appeal to the players.
 
Last edited:
As I have a lot of trouble understanding all your functions, I have your method 2. Duplicating the most of the debug I just remove the debug.
I did this :
Could you show me the actual script you wrote?
Setting the Sword Weapon in the "OnObjectImpact" makes no sense.

the
Code:
EventHandler.RegisterEvent<ImpactCallbackContext>(gameObject, "OnObjectImpact", OnObjectImpact);
already registers to the event, you don't need to create a Unity event (except if you want to, it can make it easier to add VFX, sound, etc... on impact)
What you want to do is deal damage to your tree in code by using the information from the ImpactCallbackContext within the OnObjectImpact function.


For the cursor, I do not believe we have an option to change the key to something else in the inspector. So I believe the best option is to write a script that enables the cursor for you when you need to
 
Good morning,
For the mouse cursor after applying your updates, in your scene demo, if I deactivate the Cursor, and in game mode, if I open a panel finally the Cursor Re appears and re -disappeared when I close it. , suddenly I base myself on this version now for my work. Thank you,
I don't know if you did a few things, I feel like you've touched a parameter in the Inventory Canvas. But in any case thank you.

And for the Farm system possible with just the weapons wanted, I always document myself, not having really understood how to do :)
 
and for the test for my tree i have test this (with "chat gpt").

Code:
namespace Opsive.UltimateCharacterController.Demo_Objects

{

    using UnityEngine;

    using Opsive.UltimateCharacterController.Items;

    using Opsive.UltimateCharacterController.Items.Actions;

    using Opsive.UltimateCharacterController.Items.Actions.Impact;

    using UnityEngine.Events;

    using EventHandler = Opsive.Shared.Events.EventHandler;

    using Opsive.Shared.Events;



    public class CustomDamageReceiver : MonoBehaviour

    {

        [SerializeField] protected UnityEvent m_OnObjectImpact;

        [SerializeField] protected bool m_DebugLogImpact;



        [SerializeField] private GameObject[] allowedWeapons; // La liste des objets qui peuvent causer des dégâts.





         public void Awake()

         {

            EventHandler.RegisterEvent<ImpactCallbackContext>(gameObject, "OnObjectImpact", OnObjectImpact);

         }



        public void OnObjectImpact(ImpactCallbackContext ctx)

        {

            if (IsAllowedWeapon(ctx.ImpactCollisionData.ImpactGameObject))

            {

                var damageable = ctx.ImpactCollisionData.ImpactGameObject.GetComponent<SimpleDamage>();

                if (damageable != null)

                {

                    damageable.ApplyDamage(ctx);

                }

            }



            if (m_DebugLogImpact)

            {

                Debug.Log(ctx);

            }



            m_OnObjectImpact.Invoke();

        }









        private bool IsAllowedWeapon(GameObject gameObject)

        {

            foreach (var weapon in allowedWeapons)

            {

                if (gameObject == weapon)

                {

                 return true;

                }

            }



            return false;

        }





         public void OnDestroy()

         {

            EventHandler.UnregisterEvent<ImpactCallbackContext>(gameObject, "OnObjectImpact", OnObjectImpact);

         }

    }

}

But i have a problem with "ApplyDamage" it's not a good reference.
 
Last edited by a moderator:
A few things are kind of off.

First of all the allowedWeapons serialized field. This souldn't be an array of gameobject. Instead make it a list of ItemDefinitions or ItemCategories.
A prefab gameobject does not equal an instance of the prefab, so your check will simply not work.

Also you are comparing it with the "ImpactGameObject". That's the tree, not the weapon. You can find the ItemDefinition of the weapon using:
Code:
ItemDefinitionBase itemDefinition = ctx.CharacterItemAction?.CharacterItem?.ItemDefinition

Getting the SimpleDamage script does not make sense. since you are dealing the damage yourself with this cusotm script you are makeing, you most likely don't have a SimpleDamage module.

ApplyDamage does not exist.

Instead you can damage your Tree by getting its health or damageable component and deal damage to that.

If you continue to struggle with this, do let me know and I'll review the code. Perhaps I need to change something to make it simpler to use
 
No, I don't think I can do it. I tried by reading a little bit of those two pages which try to do something similar, I think.
https://www.opsive.com/forum/index.php?threads/onobjectimpact-event-not-firing.1418/
https://www.opsive.com/forum/index.php?threads/real-blood.3742/

I asked Chat GPT so many things (poor thing), I even gave it pieces of code with the definitions for health and onobjectimpact. But it always makes errors.
Here is my latest test, but it gets stuck on Hitcollider, which it doesn't recognize for the ImpactCallBackContext class.
To you, this must be nonsense.


C#:
using UnityEngine;
using UnityEngine.Events;
using Opsive.Shared.Inventory;
#if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER
    using Opsive.Shared.Networking;
    using Opsive.UltimateCharacterController.Networking.Traits;
#endif
using EventHandler = Opsive.Shared.Events.EventHandler;
using Opsive.UltimateCharacterController.Items.Actions.Impact;
using Opsive.UltimateCharacterController.Traits;

public class CustomDamageReceiver : MonoBehaviour
{
    [SerializeField] protected UnityEvent m_OnObjectImpact;
    [SerializeField] protected bool m_DebugLogImpact;
    [SerializeField] protected ItemDefinitionBase[] m_AllowedWeapons;

    private void Awake()
    {
        EventHandler.RegisterEvent<ImpactCallbackContext>(gameObject, "OnObjectImpact", OnObjectImpact);
    }

    private void OnObjectImpact(ImpactCallbackContext ctx)
    {
        // Vérifiez si l'arme est autorisée
        if (IsAllowedWeapon(ctx.CharacterItemAction?.CharacterItem?.ItemDefinition))
        {
            // Obtenez le composant endommageable de l'arbre et infligez des dégâts
            var health = ctx.HitCollider.GetComponent<Health>();
            if (health != null)
            {
                health.Damage(ctx);
            }
        }

        // Déclenchez l'événement UnityEvent
        m_OnObjectImpact.Invoke();

        // Afficher un message de débogage si nécessaire
        if (m_DebugLogImpact)
        {
            Debug.Log(ctx);
        }
    }

    private bool IsAllowedWeapon(ItemDefinitionBase itemDefinition)
    {
        // Vérifiez si l'ItemDefinition de l'arme est autorisé
        foreach (var allowedWeapon in m_AllowedWeapons)
        {
            if (allowedWeapon == itemDefinition)
            {
                return true;
            }
        }
        return false;
    }
}


But for me, it's a bit like Chinese. I'm trying to understand. Yet what I want to do, in my imagination, is simple, but if you don't know all the definitions and the code simply, it's torture.
I've already spent 6 months trying to understand 10% of UCC.
Now I understand a little bit about Inventory. I'm using what's already created to copy and paste a new one and make it work.
But since you haven't done the UIS integration demo like the UCC demo with all the weapons, and the UIS integration demo doesn't have everything like the normal UIS, the depot or upgrade, for example (which I haven't done yet, failed once).

I feel like I'm spending my months just trying instead of moving forward using the demos.
I've watched all the videos several times, but since I'm watching your actions with my eyes and not listening, since I don't understand spoken English, I think you sell your projects saying that people must know a minimum.

But since it's super complex to understand, as soon as we have to do our own code, it's a headache. Except for developers.
Sorry for this long text, I wanted to tell you my feeling as an amateur and beginner in Unity.
 
I understand what you mean. both UCC and UIS are very big with a ton of functionality. It takes time to understand the ins and outs.

But using Chat-GPT is not a good approach. It doesn't have enough context to understand the code structure of either of our tools.

If game dev is something you plan to stick to for awhile, it might be a more efficient use of your time to learn about C# by taking a course or two. There are some pretty good in depth ones for free on youtube. Just having the right IDE (I'd recommend viusal studio (not visual studio code), or for proffesional programmers I recommend Rider, that's what I use) and learning some few tips on how to read code and find where classes and function are used will get you a long way


Coming back to your issue. You've been struggling with this for awhile and what you are trying to do is something other people might find useful. So I'll implement it for you. I'll add some additional functionality to make it more generic and easily customizable.

I'll add all those changes for the next update.

As soon as I am done I'll let you know and I'll send you the relevant scripts so that you won't have to wait for the update to try them out.
 
Thank you greatly!
And since the beginning of the year I have been following a French Youtubeur who explains how to do things on Unity and the C#.
He showed us how to make an inventory, a farm, craft, construction. He explains each line he writes. Thanks to that I already understand some things.
 
So I ended up spending all day on it. I added a new feature that allows you to define ImpactActionConditions within the ImpactAction. And you can set conditions both from the source and the target.
I added quite a few condition built-in, for categories, definition attribute values, etc...

I wrote a new documenation page to explain how it works in detail.

And writing a script to add a new condition is super easy

Unfortunatly since it meant adding a lot of new scripts and making some changes to existing ones I would rather you wait for the next update. I'll see with Justin if we can make one in not too long.
 
Thank you, no problems.
There are so many things to do while waiting, that it doesn't bother me.
Already thank you for taking your time for that.
I also think that this system can interest the opsive community
 
Good morning,

I could see that the new script we were talking about was present.
Thank you for your work and listening to the wishes the community.


Your "Conditionalimpactreceiver" script which is I suppose the script which manages the impact caused by only a category of weapons, does not seem to work anyway.
I manage to touch the test model with all the "non -melee" weapons in the demo, while the script with your example should blocked the degats suffered by "non -melé" weapons.

In your tests it worked?

Maybe it would be better to do them by "itemdefintions" than by "itemcatégorie" (in any case I created in my test a category "harvest" But I could not verify if it worked, as there is already the same little problem of the demo.

Looking forward to reading you.

ff.PNG
 
I tested the scripts and they do work.
I think you make the wrong assumption.
The script does non "block" damage. It simply does the "Impact Action On Pass" if the condition passes.

So if your weapon deals damage directly from the weapon using a SimpleDamage, it will still cause damage. But if it was a melee weapon then It would cause damge twice. Once from the weapon SimpleDamage and a second time from passing the melee condition.

So if you want to not deal damage at all with the assault rifle you have 3 options.

1) Remove simpleDamage from all weapon impact and simply using an ImpactEvent action. Such that the damage is only dealt by the ConditionalImpactReciver.
The disadvantage with this approach is that you can't have damage be different per item unless you make a custom impact event action.

2) Use an ImpactActionConditions inside the weapon. This is a impact action that allows you to define a list of impact actions that happen when the condition passes. This way instead of having directly a SimpleDamage action, you would have a condition which nests a SimpleDamage.
You don't need a ConditionalImpactReciever at all with this approach.
The disavantage of this approach is that you'll need to define it on all weapons.

3) Similar to 2) but use a ImpactConditionBehaviour. It allows you to define a condition on the weapon, that will check the target to see if it can be hit. This allwos you to have the best of both worlds in my opinion.

As for using itemdefinitions instead of categories, you can. Simply replace the CheckweaponCategory condition by a CheckWeaponDefinition condition.

Side note, I wrote a page that explains all of this in detail, but for some reason it does no show up in the documentation. As soon as I figure out why and fix it, I'll send you a link.
 
Sorry for the confusion
I thought you had made a script that was in relation to what I try to do. To be able to destroy a type of object only with the weapons wanted.

So I always test, I even try to adapt a tutorial that used this farm system, but for the moment without conclusion.

Suddenly my hopes ... the desire ... the request that I make to you. And I would understand that you did not want it where there was no interest for you or for your community



Could you create a script (without having touched the construction of the impact system of the weapons already in place),
destroy object only some (like big stone, or trees) but i can touch other object , but not destroy.
(a system a bit like "rust" games, for example.) Can only be destroyed with a type of weapon.
I think, do duplicated file "health" and modifie it for add new code ( only weapon ".." with defintionitem choise, rigidebody desactivate after dead,


Example in the script:
- Power destroying stones only with so -called weapons (draw, pickeur hammer)
PIOCHE 1 Give 10 impact
PIOCHE 2 gives 20 impact
Picker hammer gives 30 impact
-others weapon can touch but not destroy ( for impact visual) ,(like (weapon rifle,gun.. or other melee item))

And
- Power destroying trees only with so -called weapons (ax, chainsaw )
ax 1 gives 10 impact
Ax 2 gives 20 impact
chainsaw gives 30 impact
-others weapon can touch but not destroy ( for impact visual) ,(like (weapon rifle,gun.. or other melee item))
And only for the power tree desactivated the "Rigidbody" when the tree dies to have it fell.



I think that it is to modify the "health" file, to add the "disablekinematic" clause to him on the objects wanted when they are dead.
And being able to put the item defintments of weapons wanted to be destroyed to destroy the harvestable objects.

I think that the script will detect the weapon that affects the object and if it is one of the objects itemdefinition defined in it can destroy it and give resources after its destruction.

I hope my translation is quite clear. Thank you already for you for your listening. And if you couldn't, I would understand.
 
The new scripts I added for Impact Conditions should allow you to do what you want.
You can define the ImpactCondition inside the weapon Impact Module. You can add a simple damage module nested under a condition of type "CheckTargetImpactConditionBehaviour". And then add a ImpactConditionBehaviour on your Stone/Tree to add the types of items that can damage it.
Have you read the documentation page I linked above in detail?


If you continue to struggle to set it up, let me know. I'll try to help you out more.
 
I did some test even by reading the doc again. Can't there be simpler? just put on the object that must die, the authorized weapons (s), and say that all the others are blocked. And if there is not this script. Can kill with all weapons.
If this is already the case, then I did not indeed understand.

Because you gave me three ways to do it for the assault rifle, but I feel like I have to adjust all my weapons by removing simple damage and putting a conditional impact action for each weapon.
I still did my test, but I could only make one impact on my (tree) and then no more impacts. I know I'm doing it wrong, but I'm having a hard time understanding how to do it.

Would you have time to make an explanatory video on the subject? with exemples.
I have patience if you are already too busy.


Here are my few tests:

3.PNG2.PNG

4.PNGA.PNGb.PNG
 
Last edited:
Ok.
I will be off next week, but I'll see if I can make it simpler once I come back.
Perhaps having the SimpleDamage itself optionally check for the Impact Condition Behavior.
Or Perhaps in addition to impact conditions I can make a custom shield component that changes shielding value depending on the item definition/item category used.
Perhaps that can make it simpler by having everything part of the SimpleDamage impact.

I'll let you know what solution I come up with and I'll try to add more examples in the demo scene
 
Top