#My code isn't working

1 messages · Page 1 of 1 (latest)

weak current
#

Hi, My code isn't working the way I want it to.

I want the freshAge to go up when you aren't on the paint, but it doesn't go up at all. can someone please help?

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

public class PlayerAge : MonoBehaviour
{
public float freshAge;
public GameObject paint;
public GameObject Circle;

private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("FRESH");
if (collision.gameObject.name == "paint")
freshAge = 0;
else
freshAge += Time.deltaTime;
}

}

dreamy summit
#

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
enter is only called on the frame you start colliding
there are a lot of problems with how you approach it
I recommend the following:
check out a few tutorials on jumping, because they will often have a grounded check method. often implemented as raycast you cast down, with that you can figure out on which object you stand.
call the groundedcheck method in Update(). when you detect a ground, get the name of the object. if it's paint, reset the freshAge, if not, increase it by deltaTime

weak current
#

Do you know how to make the raycast cast under the player from top down?

dreamy summit
#

oh top down

weak current
#

ye sorry forgot to specify

dreamy summit
#

in this case, you might only have to check for a point
https://docs.unity3d.com/ScriptReference/Physics2D.OverlapPoint.html
the easy method call works only for colliders, not triggers, but it seems like you can use this
https://docs.unity3d.com/ScriptReference/ContactFilter2D.html
to set filter options, like if you also want to have triggers you can set
https://docs.unity3d.com/ScriptReference/ContactFilter2D-useTriggers.html to true

you'd also have to work with layermasks to not detect the player itself

weak current
#

I'm dumb and couldn't get that working but i made something that almost works, but it stops after a few seconds

#

public class PlayerAge : MonoBehaviour
{
public float freshAge;
public GameObject paint;
public GameObject Circle;

public void OnTriggerStay2D(Collider2D collision)
{
     Debug.Log("FRESH");
    if (collision.gameObject.name == "paint")
        freshAge = 0;
    else
        freshAge += Time.deltaTime;

}

}

#

(I'm new to coding if you couldn't tell)

#

i also made the ground a trigger to get freshage to count up, but it stops after a bit

#

it works if you always move

dreamy summit
#

the problem with OnTriggerStay is that if you intersect with multiple triggers , you have that called twice every frame

but okay let's do a very simple approach. let's assume you set up your trigger properly, and you set up obects so that you don't collide with multiple at once

but also, isn't the condition inverted? you say you want it to go up when you're not on the paint?

public class PlayerAge : MonoBehaviour
{
    public float freshAge;
    public GameObject paint;
    public GameObject Circle;

    private bool isOnPaint;

    private void FixedUpdate() {
      if (isOnPaint == false) {
           freshAge += Time.deltaTime;
        } else { freshAge = 0f; }
        isOnPaint = false;
    }

    public void OnTriggerStay2D(Collider2D collision)
    {
         Debug.Log("FRESH");
         isOnPaint = collision.gameObject.name == "paint")
    }

}

https://docs.unity3d.com/Manual/ExecutionOrder.html
so in OnTriggerStay, you set a flag for if you stand on the paint or not,
FixedUpdate, which should run before your TriggerStay calls, can check this flag every frame and operate accordingly, then you can make it on a per frame basis, and you don't need OnTriggerStay to be called for your logic. because in FixedUpdate, the logic will run every physics frame. you only use the triggerStay to set if you are on the paint or not

#

since you want an operation every frame, it makes sense to use FixedUpdate or Update
because OnTriggerStay is only called when you actually have a trigger collision, which might not be the case every frame