#Need help about 2dtopdown game
1 messages · Page 1 of 1 (latest)
it is a sword attack
[]nocode
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
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.
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...
I am trying to do this thing at 38:00
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
Ok, so :
- 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;
- Do you have rigidbody2D component to at least of the interacting objects?
- Do your player and slime have colliders? And if so, make sure they aren't triggerd.
oh
the player's collider was trigger
I fixed it
and it started working
thank you so much
I have to ask last thing
I'm all ears
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;
sorry my for my poor English I meant where should I put this "[SerializeField] private float swordDamage = 1f;" in my code
oh
okay
I thought I should write both of them
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
Nope, you would even get an error if you try to duplicate it but make one public and the other one serialized
okay
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"
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.
You should never have fields read by another class, so they should never be public to begin with
I am little confused right now... Maybe I am just dumb right now but could you explain a little bit?
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
https://learn.microsoft.com/en-us/dotnet/csharp/properties
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
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 )
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?
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
Got it, I'll maybe make one later or tomorrow. Thanks again
If it is a sword attack then it's better to let the animation call the function so that the attack should be neat
It is doing so because of the collision of 2 rigidbodies
how can I prevent this?
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
okay thx
And you can also play with the mass
In this Unity tutorial we will find a way to stop Enemy that is moving using Rigidbody from pushing Player or another object in your Unity game. I will explain how to stop Dynamic Rigidbody from pushing another one and a better way to solve it which involves a use of kinematic Rigidbody.
Design Patterns in Unity course:
https://courses.sunnyval...
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...
1:05:00
but it gives an error like this which did not happen in the video
Try maybe to put col instead of collider
it still doesn't work
I don't think a Collision2D have a GetComponent function defined. You can do col.transform.GetComponent and that should work
I am sorry but "transform" has nothing to do with this
· The Transform is just a representation of the transformation of a GameObject in terms of its position, rotation, and scale. It has nothing to do with the GetComponent<T>() or with collisions.
· For the problem you would need to return the gameObject that is involved in the collision : col.gameObject.GetComponent<IDamageble>().
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
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.
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.
-
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. -
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.
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.
I don't think so as well but it's always better to know what you're working with and what you're doing than just "if it works then it's fine". And yes, both of these statements will fetch the component T from the same GameObject that was part of the collision. But I think you also want to learn and understand what you're doing here, right?
I see...
Lovely
. 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
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.
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.
No, it really wasn't relevant to my response but we'll just have to agree to disagree then.
Let's just say I agree on this one
Lovely!