Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Enable Sphere Collider On Player For Melee Attack - How To Transfer Data From Player To Enemy

Discussion in 'Scripting' started by Mahonroy, Jul 19, 2023.

  1. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    Hello, I currently have a GameObject acting as the player, with a few child GameObjects - one of which I named "AttackShape" and assigned a Sphere Collider to it.

    My player code contains attack logic, and also contains attack info (such as damage, knockback force, etc.). What I was hoping to do was during an attack sequence, to reference the sphere collider and set it to enabled, so that if it intersects with an enemy, it will trigger the "OnTriggerEnter" function in the enemy's code, and I can deduct damage, apply knockback forces, etc.

    The problem is I don't know how to pass this data into the collider object, or even the enemy code for that matter.

    I do have a "GameManager" script where I could put some of the damage related variables, and in the OnTriggerEnter function within the Enemy, I could then reference the damage variables globally from the GameManager..... but I see several problems with that.... one of which is what if I later make an arrow that deals small amount of damage, player launches that arrow, then switches weapon to a high damage weapon, as soon as the arrow lands, the enemy would receive the high damage, which would be incorrect. It seems the damage information needs to be tied to the collider object to retain accuracy here.

    Any help or advice on this would be greatly appreciated, thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Most approaches involve using GetComponent<T>() on the target object, which you get from the raycast / overlap.

    Look at some video tutorials for hitting... there's no point in me retyping all the gory details here.
     
  3. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    Thanks for the response!
    I have been watching a bunch of misc Youtube videos on colliders, but they are not addressing the sending of data part of the problem.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    An option could be to have the damage information on the weapon itself.
    When the enemy is hit, check (with code on the enemy) if it was hit by a weapon, if so, read the damage the weapon causes from the weapon information, and update the enemy stats.

    You may want to look at Scriptable objects for creating a weapon system
     
  6. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    Thanks for the response!
    I like the idea of putting the damage information on the weapon/collider object - how do I pass that damage information from the player over to the weapon/collider object, so that the enemy can read it after the collission?
     
  7. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    when the enemy is hit, you can check what hit him, in this case the weapon, at that point all information of the weapon is available

    Do a search on collisionenter or triggerenter. Lots of videos on that.
     
  8. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    This won't work because the damage information is contained within the player, not the weapon.
     
  9. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    it is code, right? Everything can be changed.
    My suggestion was to change your code logic.
    Are you using your own code, or code you copied from a tutorial?
     
    Kurt-Dekker likes this.
  10. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    577
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Sword : MonoBehaviour
    6. {
    7.     void OnTriggerEnter(Collider other)
    8.     {
    9.         AlienManager am=other.GetComponent<AlienManager>(); // get the alien's script if any.
    10.         if (am) // does the other object even have an alien script? We may have bumped into somebody's granny for all we know!.
    11.         {
    12.             am.DoDamage(10); // call the public method within the alien's script that handle's damage
    13.         }
    14.     }
    15. }
     
  11. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    My own code.

    I can put damage information in the weapon/projectile/etc., I just need to be able to update/change the info from the player controller, game manager, etc. How would I access that info prior to the collision happening, to make sure it has the correct info first?
     
  12. Mahonroy

    Mahonroy

    Joined:
    May 14, 2022
    Posts:
    14
    Thanks again for the response. I implemented an interface as a test:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public interface IDamageable
    6. {
    7.     void ReceiveDamage( float damageAmount );
    8. }
    9.  
    Basically what I did was create a prefab called "MeleeAttack", it has a sphere collider, and "MeleeAttack" script attached to it:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MeleeAttack : MonoBehaviour
    6. {
    7.     private void OnTriggerEnter( Collider other )
    8.     {
    9.         Debug.Log( "Trigger on Melee Attack code:" + other.name );
    10.  
    11.         IDamageable iDamageable = other.gameObject.GetComponent<IDamageable>();
    12.         if( iDamageable != null )
    13.         {
    14.             iDamageable.ReceiveDamage( 9 );
    15.         }
    16.     }
    17. }
    When spacebar is pressed, I instantiate the MeleeAttack prefab, and place the object right ontop of the player.

    This works great..... but the problem I am running into, is at the point of me instantiating the MeleeAttack prefab, I need to specify the "9" variable. I have it hard coded right now as "9", but I need to set this at the time its instantiated.

    Do you know how to do this? Thanks again for all of the help!

    EDIT:
    I figured it out - I made a public variable under MeleeAttack called "Damage", then did this:
    Code (CSharp):
    1.  
    2. GameObject go = Instantiate( _meleeAttack, transform.position, Quaternion.identity );
    3.             MeleeAttack meleeAttack = go.GetComponent<MeleeAttack>();
    4.             meleeAttack.Damage = 50;
    5.  
     
    Last edited: Jul 19, 2023
    Kurt-Dekker likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Make it a public field you can set in the Melee.

    Or if you want it random, choose a random hit and use that.

    Code (csharp):
    1. // deal 2d6+4 damage
    2. int damageThisSwing = Random.Range( 1, 7) + Random.Range( 1, 7) + 4;
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Feels good, doesn't it? This is the core of gamedev... layering one thing on top of the next, building up steadily.

    Be sure you're backing everything up properly on a regular basis so you don't suffer the crushing agony of lost work. I do recommend installing source control, but it has its own learning curve. At a bare minimum, zip the entire Assets folder once in a while and keep those backups.

    PROPERLY CONFIGURING AND USING ENTERPRISE SOURCE CONTROL

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    I usually make a separate repository for each game, but I have some repositories with a bunch of smaller test games.

    Here is how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally the ONLY folders you should ever source control are:

    Assets/
    ProjectSettings/
    Packages/

    NEVER source control Library/ or Temp/ or Logs/
    NEVER source control anything from Visual Studio (.vs, .csproj, none of that noise)

    Setting git up with Unity (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It's simply ridiculous not to back up.

    If you plan on joining the software industry, you will be required and expected to know how to use source control.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards