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. Dismiss Notice

Question Affecting value on other object independant of who is player 1 or 2

Discussion in 'Scripting' started by NetplayTactics, Oct 23, 2022.

  1. NetplayTactics

    NetplayTactics

    Joined:
    Jan 8, 2021
    Posts:
    3
    I'm working on doing a test project for learning how to make my own fighting game. My current idea is to set up character prefabs to instantiate based on what is chosen at character select.

    One issue I ran into was needing objects separate from the character with their own colliders to set up changing the colliders in any detail during keyframe animation (since if there are multiple box colliders editing any of them in keyframe recording only applies effects to the topmost collider).

    Additionally I am using "hurtbox" and "hitbox" tags on these objects; hurtboxes are where the player can be hurt, and hitboxes do the "hitting". How would I have a player's hitboxes avoid hitting their own hurtboxes and only affect their opponent's?

    Hiearchy
    Screenshot_4.png

    Hitbox
    Screenshot_5.png

    Hurtbox
    Screenshot_6.png
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The character and the player controlling them should be independent of each other.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    It looks like one player's hitbox overlaps the same player's hurtbox, so it will trigger an OnTriggerEnter. I don't know your GameObjects are set-up but you could use GetInstanceID or just the gameObject.name to find out whether it's the correct object or not.

    I did this once for one of my own projects- each bullet stored the instance ID of the character that shot it. Then whenever the bullet OnTriggerEnters with a character, the bullet compares the stored instance id to the instance id of the object that it collides with. If they are different then the bullet damages the character and the bullet is destroyed, but when they are the same than nothing happens.
     
  4. NetplayTactics

    NetplayTactics

    Joined:
    Jan 8, 2021
    Posts:
    3
    From my understanding the boxes all need to be on the same layer, but avoiding self-overlap is impossible for general functionality and balancing reasons.

    My plan in the meantime was to clone the character prefabs and change tags etc for Player 2 to instantiate instead, which doesn't actually look like that much more work. Looking for the gameObject.name doesn't seem nearly as efficient as tags in this case, but I'm still building experience so I wouldn't really know better.

    GetInstanceID sounds like it it'll fix things and save me the extra work with the prefab cloning. I should dig harder through the documentation even if it's daunting, thanks.