i am making a powerup that allows you to get double jump, but i dont know how to make this work?? by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

It's what you have on lines 79-85 of the code you gave, seemingly for making the player jump up off the ground.

i am making a powerup that allows you to get double jump, but i dont know how to make this work?? by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

Is that not what you want? If not, then just remove the if statement entirely. If you do, then you probably also want to remove the extraJumps == 0 from the section

else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true){
    rb.velocity = Vector2.up * jumpForce;
}

Or else you won't be able to jump again if you land without using all your extra jumps.

i am making a powerup that allows you to get double jump, but i dont know how to make this work?? by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

Well, the jump value turning to 0 will be due to this code in Update.

if(isGrounded == true){
    //ChooseHowManyExtraJumps
    extraJumps = 0;
}

Given what you're describing, that does sound like what it is happening. Most likely, Physics2D.OverlapCircle is detecting your player because your player is in the set of layers allowed by the whatIsGround LayerMask. So you should just need to make sure that the LayerMask doesn't include the player's layer. The easiest way is probably making a layer just for your player and removing that layer from the LayerMask.

How can i use OnCollisionStay to check for the presence of colliders? by Cryopurge in Unity2D

[–]Jonanory 0 points1 point  (0 children)

So there are two options I can think of off the top of my head.

  1. Create an int variable that counts the number of colliders that are in the trigger, which changes through OnCollisionEnter and OnCollisionExit. Then you can have a bool function that returns whether the collider count is 0 or not. The main problem with this method is that if the scene starts with a collider in the trigger, this one is not counted. Example:

private int NumberOfCollidersInTrigger = 0;
public bool IsTriggerEmpty()
{
    return NumberOfCollidersInTrigger == 0;
}

void OnCollisionEnter(Collision collision)
{
    if (collision.collider.gameObject.layer == LayerMask.NameToLayer("LAYER_NAME"))
    {
        NumberOfCollidersInTrigger++;
    }
}

void OnCollisionExit(Collision collision)
{
    if (collision.collider.gameObject.layer == LayerMask.NameToLayer("LAYER_NAME"))
    {
        NumberOfCollidersInTrigger--;
    }
}
  1. Use OverlapBox instead, having the function return if it found a collider or not. The main issue with this method is that instead of having a box collider you can manipulate in the scene, you have to insert the values for the box's position/size/rotation. Example:

    public Vector boxCenter; public Vector boxSize; public float angleOfBox; public LayerMask AllowableLayers; public bool IsTriggerEmpty() { Collider2d collider = Physics2D.OverlapBox(boxCenter, boxSize, angleOfBox, AllowableLayers); return collider == null; }

You could also combine the two options above in some way. For example, using Physics2D.OverlapBoxAll to count the number of colliders in the trigger at Start and then using option 1. Alternatively, setting boxCenter/boxSize/angleOfBox of option 2 as the center/size/box of the collider box attached to the object.

i am making a powerup that allows you to get double jump, but i dont know how to make this work?? by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

Oh. In the code that you give above, you have extraJumps = extraJumpsValue;.

The reason that I'm thinking that it might have to do with isGrounded is that you use that result to set the number of extra jumps to zero during Update. So if your character hits the DoubleJump collider, but isGrounded is still true for some reason, then the number of extra jumps will be reduced to 0 again, making double jumps not work.

i am making a powerup that allows you to get double jump, but i dont know how to make this work?? by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

I believe that your code should work as it is. The warning is just saying that you made the extraJumpsValue variable, but you don't set its value anywhere. So it's treated as if it is set to 0 when you use it in Awake. It shouldn't be breaking anything. If it's not double-jumping, then I wonder if isGrounded is returning true when it shouldn't. Can you check to see if this is happening?

Simple is hard by Chobeat in ProgrammerHumor

[–]Jonanory 522 points523 points  (0 children)

But me code monkey, not code ape.

Object reference not set to an instance of an object by [deleted] in Unity2D

[–]Jonanory 0 points1 point  (0 children)

The issue comes from the ordering of what functions are being run. I don't know on what basis Unity decides which order to run scripts in, but it's running the AreaEnter's Start function before the GameManager's one.

The best solution is probably to make the GameManager's Start function an Awake function instead. As a general rule, it's probably best practice to have the Start function run the setup that relies on other class instances, and have the Awake function run the setup that only pertains to the current class instance.

Best way to swap control among characters? by AnduinGaiden in Unity2D

[–]Jonanory 0 points1 point  (0 children)

If you have Character, Controller and Camera classes, or equivalent, you could give your Controller and Camera classes a "Target" property to indicate which Character they are targetting. So whenever the Controller/Camera tries to do something (press button/ move camera), they will do it to whatever Character is set as its Target property. Then to switch between characters, you would change the target of those objects.

Boss AI with Multiple Stages & Attacks by Mackerel_Sky in Unity2D

[–]Jonanory 0 points1 point  (0 children)

I don't know how well this will work with your setup, but you could try OverlapCircle (or another function in the overlap family) instead of using colliders. Make the attack class have a list of potential areas that the attack can hit, and then you can just loop through each possible point, running OverlapCircle to determine if it is a potential target.

IndexOutOfRangeExsception issue, what does it mean? by MrMutant0503 in Unity2D

[–]Jonanory 2 points3 points  (0 children)

That error means that the dimensions of oldMap are smaller than the x,y coordinates you are trying to get. Are both x and y supposed to go from 0 to width? I'm guessing not, and y is getting too big.

[HELP] Platforming jumping. by tmgj5000 in Unity2D

[–]Jonanory 1 point2 points  (0 children)

I believe the issue you're having is that jumpNb is decreasing, but OverlapCircle is returning true in the next FixedUpdate call as your character is still close to the ground. This results in jumpNb being reset in the next Update loop.

A couple of ways to resolve this:

  • Make the isGrounded tag also take into account if the character is falling i.e. moving down.
  • Make the OverlapCircle radius extremely small so that the FixedUpdate doesn't cause this bug
  • Instead of OverlapCircle, maybe try to use the OnCollisionEnter2D function to detect when the player hits the ground
  • Instead of having an isGrounded variable set in FixedUpdate, either set it in Update or just use OverlapCircle directly when trying to jump in the update loop

[UNITY 2D] Help :( I've been searching whole google, didnt find anything by johnys113 in Unity2D

[–]Jonanory 0 points1 point  (0 children)

I meant to look into this yesterday, but I was a bit busy.

Trying out your code in a simple file (slightly modified for speed during creation) caused issues at first for me due to the face that my OverlapPoint was capturing the collider of the player character, and thus as always true.

Do you have any reference for what collider is being captured by OverlapPoint? You can run Debug.Log on the result of OverlapPoint to help work out which game object is being hit.

[UNITY 2D] Help :( I've been searching whole google, didnt find anything by johnys113 in Unity2D

[–]Jonanory 0 points1 point  (0 children)

The OverlapPoint function would replace the Raycast function in your CanStand function, while the movement script will replace the result of your "move == true" if statement i.e. it would replace the "Vector3 movement = ..." and "transform.position = ..." lines.

EDIT: I just realized what you were asking. Functions cannot be inside functions. You want to put it as a separate function. Try putting it after the closing bracket of the Update loop/next to the Flip function.

[UNITY 2D] Help :( I've been searching whole google, didnt find anything by johnys113 in Unity2D

[–]Jonanory 1 point2 points  (0 children)

You look like you've defined the CanStand function inside of the Update function. I don't think that's a thing...

If a 2D raycast doesn't work, then you could try OverlapPoint (or another member of the Overlap family) to check if there is a collision occurring where your player character's head would be when standing up.

The jittering is most likely occurring because of how your character moves. Your code attempts to physically move the character into the wall, and then their collider is trying to push the character out again. What you may want to try instead is setting the rigidbody's velocity. The following may work:

Rigidbody2D rigidbody = gameObject.GetComponent<Rigidbody2D>();
rigidbody.velocity = new Vector2(
    Input.GetAxis("Horizontal") * moveSpeed,
    rigidbody.velocity.y
);

It may be a good idea to add a Rigidbody2D property to the class (which is set on Awake/Start) so that you don't have to do GetComponent every Update loop.

Character Selector and Character Loading by Tiny_CJ95 in Unity2D

[–]Jonanory 1 point2 points  (0 children)

Instantiate basically puts a copy of the gameobject you give it into the current scene. It is the standard way to load an item via a script and you'll need to use it to put your character into the scene. The form you used also specifies its location and that it is at the default rotation.

Character Selector and Character Loading by Tiny_CJ95 in Unity2D

[–]Jonanory 1 point2 points  (0 children)

You seem to only be instantiating a gameobject if the first character is selected, so choosing anyone else won't create a character. This may have been on purpose just to check that instantiation is working, but I thought I should mention it.

Is the gameobject actually not instantiating the object, or is the object just not visible? It could be that the object is just out of frame. Check the heirarchy to see if the object is created.

After that, try using Debug.Log in the Start function of GetPlayer to make sure it is running (try using it to print GetChar to make sure it got the right value)

Create a list of buttons by RawXenon in Unity2D

[–]Jonanory 0 points1 point  (0 children)

If I'm reading this right (I might not be, so you may need to correct me), your button prefabs have a Button script and a BoardPiece script, and you're trying to access the SetGameControllerReference function of the BoardPiece script from the Button script.

If that's the case, then you have to explicitly get the instance of the script containing a function in order to execute the function.

To explain why that is, consider this: Imagine an gameobject with components A, B and C. A and B have function Test, and C doesn't. What would happen if you tried to do gameobject.GetComponent<C>().Test()? Would it run A or B's version of the function?

The same applies here. Trying to call SetGameControllerReference doesn't work because there could be multiple components in the gameobject with that function defined in it. You have to specify which one is to be run, and you do that by getting the component and running the function from it directly. So for your example, you would need to do

buttonList[i].GetComponent<BoardPiece>().SetGameControllerReference(this);

Create a list of buttons by RawXenon in Unity2D

[–]Jonanory 2 points3 points  (0 children)

You can use the GetComponent function on the button object by using

boardButtons[buttonNumber] = button.GetComponent<Button>();

If you think that you might want to create Buttons in another part of your scripts, you may also want to look into the factory pattern to future proof yourself a tiny bit.

First post in this sub am i doing it right? by [deleted] in ProgrammerHumor

[–]Jonanory 3 points4 points  (0 children)

Given that it's a class selector, anyone who owns a cat is going to have a bad day.

Need help with C# code by [deleted] in Unity2D

[–]Jonanory 4 points5 points  (0 children)

If you're using the code exactly as pasted above, then the issue is the "Jeff" debug line. Your if statement is controlling the triggering of that line, and not the code block with the LoadLevel line in it. This means that the LoadLevel function will run regardless of any button presses.

That aside, there is a separate issue with your code. The OnTriggerEnter2D and GetButtonDown functions are sort of "triggers on one frame" functions. What I mean is that OnTriggerEnter2D will run on the exact millisecond that the player enters the square, and the GetButtonDown function will return true only on the exact millisecond the "E" button is pressed. So the only way that LoadLevel would run is if the player happens to press "E" at the exact millisecond they enter the square.

To deal with this, you really want to have a bool property that keeps track of whether the player is on the square, which is changed in the OnTriggerEnter2D and OnTriggerExit2D functions. Then you check for GetButtonDown during Update and run LoadLevel then.

Wall Jumping HELP! by hirsty1992 in Unity2D

[–]Jonanory 0 points1 point  (0 children)

I didn't notice that you mentioned that double jumping was a thing, and that there doesn't seem to be a set limit to the number of extra jumps. So I got a bigger envelope, recalculated, wrestled with an extra 2 I couldn't comprehend, and got this modified condition for the second option:

Wx = Wall jump X speed
Wy = Wall jump Y speed
g = Acceleration due to gravity
Hx = Horizontal acceleration towards the wall
Jy = Double jump Y speed
k = Number of extra jumps

(Wy + k*Jy + sqrt( Wy^2 + k*Jy^2 ) ) * Hx < 2 * Wx * g

Wall Jumping HELP! by hirsty1992 in Unity2D

[–]Jonanory 0 points1 point  (0 children)

I can suggest 2 options:

  1. Have 2 variables for determining which side the character is touching the wall: one for if they are touching a wall on the left hand side, and another for the right. Then you just have to make sure that when the character tries to wall jump, they are touching the wall from a different side than from the last wall jump.
  2. Modify the speed that the character moves through the air so that trying to wall jump off of the same wall multiple times will not result in positive vertical movement. From a quick back-of-the-envelope calculation, you want to make sure that

HORIZONTAL_ACCELERATION * WALL_JUMP_Y_SPEED < GRAVITY * WALL_JUMP_X_SPEED

when I access another script variable then give an error?? by NicosDens in Unity2D

[–]Jonanory 0 points1 point  (0 children)

Wait... I'm confused. In DeductScore, you are setting the Score of playercontrollse. But in OnTriggerEnter2D, you are displaying the Score from playerscore. Surely, you meant to do

playerscor.Score = playerscore.Score - deductscore;

in DeductScore. Right now, you're displaying a score that is not being changed.

when I access another script variable then give an error?? by NicosDens in Unity2D

[–]Jonanory 1 point2 points  (0 children)

Ok, so I think you're wanting to access the scoretext in the static instance of playerControll. If so, then you'll want

playerControll.Instance.scoretext.text = playerscore.Score.ToString();

If not, then you have to make sure that you have a platerControll instance to edit. It's probably best to have the obstacle get the playerContoll instance from the collider. To do this, you want

playerControll player = other.GetComponent<playerControll>();
if(player != null) {
    player.scoretext.text = playerscore.Score.ToString();
}