#can anyone help me with debugging my

1 messages · Page 1 of 1 (latest)

silk wing
rare orchid
#

@silk wing What's the issue exactly?

silk wing
#

the left attack dosent work

rare orchid
# silk wing the left attack dosent work

What? I see you're using Physics2D.OverlapCircleAll which has no left & right.
Are you trying to cast the sphere left or right depending on character facing direction?

rare orchid
# silk wing yep

You calculated this value, but later on you don't use it in the overlap circle?
Vector2 attackPosition = (Vector2)attackDirection.position + lastDirection * attackRange;

Physics2D.OverlapCircleAll(attackDirection.position, attackRange, detectEnemy);

silk wing
#

im confused

#

oh shit

#

i should change it to attackPosition

#

still didnt work...

#

thank you for your help btw im really dumb and dont know how to code so i may not know what to do even when u give me clear answers lol

#

apologizing in advance

rare orchid
# silk wing wait is this the solution

I recommend you start using OnGUI for debugging - it'll show values in real time & less laggy than Debug.Log

{
        GUI.Label(new Rect(10, 10, 100, 20), "My Cool Variable = " + myCoolVariable);
}```
#

Basically let's make sure your lastDirection works before we go into the other stuff.

silk wing
#

the left direction returns correct, (-1,0)

#

i think it has to do with the attack math or the shape of the attack

#

im so stuck bro

#

fuck

rare orchid
#

Nah definitely not... This is extremely simple, we just need to determine at which point things went sideways.

silk wing
#

(0,0)

rare orchid
#

When you multiple (0,0) by the attackRange, you will get (0,0)

silk wing
#

wait

upbeat wasp
#

too many things happenig in one frame (when you press space key) i dont know if that is a good idea

silk wing
#

Attack input detected
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:37)

#

lastDirection: (0.00, 0.00)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:40)

#

attackDirection.position: (0.75, -3.08, 0.00)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:41)

#

attackRange: 0.6
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:42)

#

Attack Position: (0.75, -3.08)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:48)

#

Enemies found: 0
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:53)

#

thats right

#

and left is

#

Last Direction: (-1.00, 0.00)
UnityEngine.Debug:Log (object)
Movement:Update () (at Assets/Movement.cs:33)

silk wing
#

Attack input detected
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:37)

#

lastDirection: (-1.00, 0.00)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:40)

#

attackDirection.position: (0.68, -3.08, 0.00)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:41)

#

attackRange: 0.6
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:42)

#

Calculated attackPosition: (0.08, -3.08)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:46)

#

Attack Position: (0.08, -3.08)
UnityEngine.Debug:Log (object)
attack:Update () (at Assets/attack.cs:48)

rare orchid
#

I thought you only want, "character is facing left" or "character is facing right"

silk wing
#

yeap

silk wing
rare orchid
#

Too many variables, too much bloat

rare orchid
# silk wing yeap

So how do we know if the attack should happen on left or right side?

#

Would it be if (attackPosition.x < 0.0f) { // Left side stuff } ? Since right is 0 or greater?

#

Are you flipping your character sprite to face left or right too? Based on this?

silk wing
#

yea this Vector2 attackPosition = (Vector2)attackDirection.position + lastDirection * attackRange;
returns attack position based on direction

rare orchid
#

This whole thing is way too overcomplicated. There's only like 3 variables at play

silk wing
#

i have an overlap circle that starts on the right side of my character

#

and its moving that

#

im gonna be completley honest with you this is the result of like 3 tutorials mashed together

#

idk jack shit about unity and im tryna make up for it with basic js and python knowledge

#

i did read the api

#

which helps a bit

#

sorry bro 😭

#

attack position. direction tells u where the attack originates and last direction is (0,0) or (-1,0), right and left respectivley

#

attack range is a float thats set to 2

rare orchid
#

@silk wing

float attackRangeAdjustedToDirection = -attackRange;

if (characterFacingRight)
{
    attackRangeAdjustedToDirection = attackRange;
}

Vector2 overlapCirclePosition = characterPosition + new Vector2(attackRangeAdjustedToDirection, 0.0f);

// or, = new Vector2(characterPosition.x + attackRangeAdjustedToDirection, characterPosition.y);

// Then whatever else
Physics2D.OverlapCircleAll(overlapCirclePosition, attackRange, detectEnemy);
#

Is this making sense?

#

You have everything ready for this, just need that characterFacingRight condition.

silk wing
#

so if character faces right, the attack would be the default value or the current position, which is right since the attack sphere is currently placed right

rare orchid
#

Yeah. Basically you just need to change 1 variable, the X offset

silk wing
#

and since the attack range adjusted to direction reverses the attack range

#

istead of changing the position of the actual sphere

rare orchid
#

Yep just flip the value if doing it left

silk wing
#

youre just changing the y to -1

#

damn

#

howd i not think of that

rare orchid
#

I'm still using overlapCirclePosition so, that's the computed "final adjusted value"

silk wing
#

im not sure how id implement this, would i just add it to my current code?

rare orchid
#

Well first of all can you just Debug.Log "Left" or "Right" instead of that whole ass Vector value please.

silk wing
#

lmao i just got rid of the debug log part in its entierty

#

mb for the vector thing

rare orchid
#

Maybe it's like this? I'm not sure how you're doing this exactly

if (attackDirection.x < 0.0f) 
{
    Debug.Log("Left attack");
}
else 
{
    Debug.Log("Right attack");
}
#

The main idea here is, we need to verify the reliability of your "character is facing direction" stuff
Before we go and start doing more with that like OverlapSphere or calculating positions

silk wing
#

not sure just doing attackDirection.x would work

#

is that how you read vector values?

#

let me check

rare orchid
#

Yeah Vector2 is x,y Vector3 is x,y,z
like myCoolVector.x

silk wing
#

ohhh

#

itd be attackPosition.x then i think

#

lemme try running it

rare orchid
#

Yes let's get that "Left attack" and "Right attack" working first 🗿

silk wing
#

Attack position 0.19 left 1.42 right

#

woops wait

rare orchid
silk wing
#

right attack outputs right attack

#

left attack outputs

#

... right attack

rare orchid
#

Okay. So, what variables can we use then, to determine which direction the character is facing?

silk wing
#

wait

#

i take it back

#

let me re run my code

rare orchid
#

Maybe the rb.linearVelocity

silk wing
#

for some reason it works fine

#

like the output left right attack

#

not the actual attack

#

the debug log

rare orchid
#

Yeah I'm saying, the log is correct.

#

We need to start with that, before the next stuff

#

Pro Tip - always verify your variables before building other stuff on top of them Unity

#

Any reason to not use lastDirection ?
Seems like that would be either positive or negative depending on the float moveInput = Input.GetAxis("Horizontal");

#

Input.GetAxis("Horizontal"); usually returns between -1 and 1 🤔

silk wing
#

sorry quick question

rare orchid
#

If you need to wait until the character is actually moving that way, rb.linearVelocity would be better.

silk wing
#

my characters are currently able to push each other

#

how would i disable that

#

would it be in rigidbody2d?

rare orchid
#

Rigidbody settings, collision layers, Include Exclude, Physics.IgnoreCollision
Those are a few ways

#

Include/Exclude is easiest, if you have your objects set to certain layers already

silk wing
#

how do i access rigidbody settings

rare orchid
#

You could use Include Layers and Exclude Layers.

silk wing
#

oh

#

i just

#

realized the problem

#

the reason it wasnt working was because when my enemy moves into my player

#

it pushes the player right

#

which changes its attack to right

#

meaning enemies coming from the left are basically unkillable

#

while that somehow didnt apply to characters coming to the left

rare orchid
#

So do you want the player to be able to attack left as soon as they start moving left? Or would they have to wait until the Rigidbody is moving left?

// Based on movement input only
if (moveInput < 0.0f)
{
}
else
{
}

// Based on if the Rigidbody changed directions already
if (rigidbody.linearVelocity.x < 0.0f)
// etc
silk wing
#

as soon as they start moving left

rare orchid
#

So you can just use the lastDirection for that

#

Make the lastDirection a public variable, perhaps add [NonSerialized] before the public keyword. This requires using System; which would go at the top next to using UnityEngine;

#

Then in the attack code...

#

JK just saw, you have a function that retrieves the value

silk wing
#

also the collision affecting direction was one thing but i still notice that sometimes it dosent register the direction correctly so gotta keep checking that

#

maybe ur current suggestions will kill all the stones w one bird

silk wing
#

illl steal leave it public for easier use

rare orchid
#

So yeah...

if (playerMovement.GetLastDirection().x < 0.0f)
{ // Left side }
else
{ // Right side }
#

give the state pattern a try - Polymorphism is super useful in OOP languages not just for character movement

#

Composition vs Inheritance - another cool related topic

silk wing
#

so like this basically?

rare orchid
#

No

#

Well... I guess sort of

#

you're using lastDirection directly as the position? I thought there's some fixed distance away that the attack checks for enemies

#

Let's think more here... What's the difference between left and right?

#

Just the "x offset" value that the OverlapCircleAll needs to use right?

silk wing
#

ur right the attack dosent work at all anymore

rare orchid
#

So that's the only difference we need in our code ...

silk wing
silk wing
rare orchid
#

No yo... The attack range is the size of the sphere right ?

#

The first parameter of OverlapCircleAll is the position to place the sphere, not the radius / range

silk wing
#

*detect enemy

primal badge
#

@silk wing you realize you're not supposed to be spamming multiple messages across multiple channels, right? Every channel I go to it's you asking about this AI generated code you're trying to debug

rare orchid
#

I'm saying, how far from the character do you want it to be

silk wing
#

also vengeful thanks for the help in advance and if ur worried about ai generated code its not lmao i can pull references for tutorials used

silk wing
rare orchid
#

@silk wing

private float sphereOffsetDistance = 1.0f; // outside of all functions
// maybe make it Serialized and add a [Range(0.0f, 2.0f)] for playmode tuning convenience
// recall that the value resets after exit playmode, use scriptable objects to solve that

// this goes after if (Input.GetKeyDown)
float sphereOffsetX = sphereOffsetDistance;
if (playerMovement.GetLastDirection().x < 0.0f)
    sphereOffsetX = -sphereOffsetDistance;

Collider2D[] DamageEnemy = Physics2D.OverlapCircleAll(new Vector2(characterPosition.x + sphereOffsetX, characterPosition.y), attackRange, detectEnemy);

#

Red is the distance (sphereOffsetX)
Blue is the radius (attackRange)

silk wing
#

for if (playerMovement.GetLastDirection() < 0.0f), the < operator cant be aplied to type vector 2 and float?

rare orchid
#

Notice that I used the character position in Physics.OverlapCircleAll
Doesn't it require that? I believe it checks an absolute position, not relative from current object or something.

#

Sorry, just add .x

silk wing
#

heres where im att right now

#

plus the .x

rare orchid
#

characterPosition does not exist, that was me writing in English

#

Maybe, playerMovement.transform.position ?

silk wing
# rare orchid

right so you control that by using the two variables sphereoffset and attackrange

rare orchid
#

Yeah so... Just finish writing the code

silk wing
#

i also forgot to delete the second collider 2d part

#

i did that

rare orchid
#

Does playerMovement.transform.position work? instead of characterPosition which is pseudocode

silk wing
silk wing
#

get last direction is a method tho?

rare orchid
#

Omg ... yo
GetLastDirection().x

silk wing
#

omg.

rare orchid
#

You're trying to run & swim before you walk

#

You know you're using the old input system too, right? Out dated as of Unity 6

silk wing
#

uhh.... no... i didnt

rare orchid
#

Alright so, let's at least finish this part

#

So delete the else { } part, move the Collider2D[] part out before the AttackCooldown part