#Need help about 2dtopdown game

1 messages · Page 1 of 1 (latest)

hazy glacier
#

I think the box collider did not touch the enemy because if you put the box collider on the entire parent gameobject or player it won't change the shape of the collider when the player will try to hit

#

It's even which type of attack ?

keen tree
mystic arrow
#

[]nocode

midnight heartBOT
#

It's hard to answer a programming question without code

Resolving a bug is almost impossible when the question doesn't include any of the buggy code. In order to help fix the problem, answerers are going to have to see what the code is.
Source: https://idownvotedbecau.se/nocode

Please isolate the problematic code and send it as a codeblock. If you don't know how to send a codeblock, type []cb

keen tree
#

okay

#

[]cb

midnight heartBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

keen tree
#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Slime : MonoBehaviour
{
void OnHit(float damage)
{
Debug.Log("Slime hit for " + damage);
}
}

#

Start from Here (Import into a New 2D URP Unity Project and then download the mystic woods art pack and put in the art directory): https://drive.google.com/file/d/13wAkLxFYcklkNKLLtpbGqRFQsZ6W4DKU/view?usp=drive_link

Import https://game-endeavor.itch.io/mystic-woods Art into the folders with the metadata

Guide on how to build a top down 2d RPG...

▶ Play video
keen tree
keen tree
# keen tree using System.Collections; using System.Collections.Generic; using UnityEngine; ...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwordHitboxRight : MonoBehaviour
{
public float swordDamage = 1f;

public Collider2D swordColliderRight;

void Start()
{
    if(swordColliderRight == null)
    {
        Debug.LogWarning("Sword Collider not set");
    }
}

// Checks for a enemy physics rigidbody and sends on hit damage to the GameObject
void OnCollisionEnter2D(Collision2D col)
{
    col.collider.SendMessage("OnHit", swordDamage);
}

}

#

this is the code of sword that should work too

mystic arrow
#

Ok, so :

  1. Just as an advice, try not to make everything "public" only things that you need you'll need them in some other scripts. If you only use it in that class and just want to see and change it's value from Inspector just Serialize it :
public float swordDamage = 1f;

---------------------------------

[SerializeField] private float swordDamage = 1f;
  1. Do you have rigidbody2D component to at least of the interacting objects?
  2. Do your player and slime have colliders? And if so, make sure they aren't triggerd.
keen tree
#

the player's collider was trigger

#

I fixed it

#

and it started working

#

thank you so much

#

I have to ask last thing

mystic arrow
keen tree
#

where should I [SerializeField] private float swordDamage = 1f;

#

this code

mystic arrow
#

I don' t really get it...

#

So, as I showed you in the code block example, insterad of typing :

public float swordDamage = 1f;

You would type :

[SerializeField] private float swordDamage = 1f;
keen tree
#

sorry my for my poor English I meant where should I put this "[SerializeField] private float swordDamage = 1f;" in my code

keen tree
#

okay

#

I thought I should write both of them

mystic arrow
#

And that's for all variables not just float, int, bool, enum, etc. If you don't need them in other class and just want to be able to see or change the value in the inspector just serialize it

mystic arrow
keen tree
mystic arrow
# keen tree so I should write "[SerializeField] private" before all my veriables?

No, sometimes you will have some variables or functions that you would need to use in another class and in order to do that it has to be "public" otherwise you can't use it anywhere else outside the class. Or sometimes there are some variables that you don't need to change the value in the inspector and don't need to use it in another class, in this case you'd leave it just "private"

keen tree
#

oh okay sorry for the trouble I am new in coding I thank you so much I have been trying to fix this for 4 days I was about to gave up.

past gate
mystic arrow
past gate
#

Using fields to publicly expose state is a huge code smell, because it can lead to so many issues down the line. If you want to expose things to other classes, that's what properties are for

mystic arrow
#

Oh... But still I want to ask, maybe it's a terrible example even more because those are enums but I'll let you have the word about it, let's say I have this class :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Toggles : MonoBehaviour
{
    public enum PlayerColor
    {
        Orange,
        Yellow,
        Blue,
        Green,
        Pink,
        Grey,
        None
    }

    public enum PlayerTrailColor
    {
        Grey,
        Yellow,
        Orange,
        Green,
        Pink,
        Blue,
        None
    }

    public enum LockState
    {
        Locked,
        Unlocked
    }

    public enum BuyState
    {
        Available,
        Unavailable,
        Bought
    }

    public PlayerColor playerColor;
    public PlayerTrailColor playerTrailColor;
    public LockState lockState;
    public BuyState buyState;
    
}

And I read the enums values in another class, is this bad as well? ( I am making this example because this is something that I am doing right now in a project so I would like to know if it's bad or not )

keen tree
#

Hi could I ask you one more question please?

#

When I hit enemy slime with the sword player gets pushed back because of sword's box collider gets enabled. How can I prevent this?

past gate
# mystic arrow Oh... But still I want to ask, maybe it's a terrible example even more because t...

I don't want to go into too much detail, since I don't want to distract from OP's question (if you want to discuss further we can have a code optimisation thread in #1007228932115931197, and I'll respond when I'm less busy).
But I'll give you two really brief pointers:

  • You shouldn't have nested types like this. Every type (enums included) should be defined in its own file. So you'd have PlayerColor.cs housing the PlayerColor enum, BuyState.cs containing the BuyState enum etc
  • Using public fields here is the same problem. TL;DR you should use a property, not public fields, to expose these variables
mystic arrow
#

Got it, I'll maybe make one later or tomorrow. Thanks again

hazy glacier
#

If it is a sword attack then it's better to let the animation call the function so that the attack should be neat

hazy glacier
keen tree
hazy glacier
#

Either you remove the rigidbody of the slime (if you are using an OnTrigger function to attack) or you code

#

I don't remember well again how to do it i will look for a youtube video and send you a link

keen tree
#

okay thx

hazy glacier
#

And you can also play with the mass

keen tree
#

I am trying to add knockback like this video https://www.youtube.com/watch?v=8rTK68omQow&t=2259

Start from Here (Import into a New 2D URP Unity Project and then download the mystic woods art pack and put in the art directory): https://drive.google.com/file/d/13wAkLxFYcklkNKLLtpbGqRFQsZ6W4DKU/view?usp=drive_link

Import https://game-endeavor.itch.io/mystic-woods Art into the folders with the metadata

Guide on how to build a top down 2d RPG...

▶ Play video
#

1:05:00

#

but it gives an error like this which did not happen in the video

hazy glacier
#

Try maybe to put col instead of collider

keen tree
#

it still doesn't work

humble wharf
#

I don't think a Collision2D have a GetComponent function defined. You can do col.transform.GetComponent and that should work

mystic arrow
humble wharf
#

OK... It looks like to me that he was trying to use a function that didn't exist. gameObject.GetComponent and transfrom.GetComponent is equivalent

humble wharf
#

Ok, thinking about it I guess gameObject.GetComponent is more "correct" since it won't have to find the GameObject attacked to the transform. But functionally they are equivalent.

mystic arrow
# humble wharf OK... It looks like to me that he was trying to use a function that didn't exist...

I don't really get what you're saying with "he was trying to use a function that didn't exist" but regarding to your last affirmation, GameObject and Transfrom are not equivalent.

  1. Transform
    · As I already said Transform component is automatically attached to every GameObject and is responsible for storing and managing the object's position, rotation and scale.
    · The Transform is essentially a way to define how the GameObject is positioned, rotated, and scaled relative to its parent GameObject or the world's origin.
    · The transform property provides easy access to the Transform component of a GameObject. For example, gameObject.transform allows you to access the Transform component of a GameObject.

  2. GameObject
    · A GameObjectis the basic unit of organization in a Unity scene. It represents any object in the game world, whether it's a character, a prop, a light source, or any other interactive or non-interactive element.
    · It is the container that holds all the components that define an object's behavior, appearance, and functionality.
    · It can have one or more components attached to it, such as transform, scripts, colliders, renderers, audio sources, etc.

So, the GameObject represents the actual object in the scene and acts as a container for various components while the Transform component of a GameObject manages the object's spatial properties (position, rotation, scale) in the game world.

humble wharf
#

Buddy, they were trying to call a GetComponent function on the Collision2D class. Whether they type col.transform.GetComponent<T>() vs col.gameObject.GetComponent<T>() is irrelevant as they will both yield the exact same result. Go try it. I don't think this OP is asking for a compare and contrast essay between the GameObject and Transform classes.

mystic arrow
humble wharf
#

I see...

mystic arrow
#

Lovely BlobCute . So it's good that we give responses here and try to help but the main goal isn't to make the OP's problem go away by spoon feeding with the direct resolve but rather trying to help him understand the problem and by giving hints and tips to help him get out of the problem

humble wharf
#

Your response was irrelevant to OP's problem... If you wanted to help him understand better perhaps explaining the difference between a Collion2D vs a Collider2D would have been much more of a relevant response than Transform vs GameObject which largely has nothing to do with OP's bug.

mystic arrow
# humble wharf Your response was irrelevant to OP's problem... If you wanted to help him unders...

It was relevant to your response to his problem. So by chaining it, it was relevant for OP too (more or less, in a way or other). Anyway I am not here to argue with you how much was this info relevant or not to the OP. I want to help him to understand what he is asking to understand and want him to understand well others responses. So while maybe a difference between a Collion2D vs a Collider2D would have been better for this problem still Transform vs GameObject are one of the most important things to know and understand in order to use Unity. If you don't know those, Collision and Colliders are out of context.

humble wharf
#

No, it really wasn't relevant to my response but we'll just have to agree to disagree then.

mystic arrow
#

Let's just say I agree on this one

humble wharf
#

Lovely!

keen tree
#

thanks for help guys

#

it stopped giving error in visual studio

#

but when I attack it gives an error like this can you explain it like I am 5 years old

#

I am new at coding