#Event Trigger lets you run methods when

1 messages · Page 1 of 1 (latest)

obtuse temple
#

I am not sure how i can trigger the car movment in my example with this Event Trigger. Because i am only using the Update and FixedUpdate functions

fast geyser
#

You would use the Event Trigger to set a bool variable to true or false

#

It will look a bit like this

#

Find the object that has the component you're interested in

#

Drag it into the field

#

Then pick the field from that dropdown and check the box to make it true (or leave it unchecked to make it false)

#

In this case, when the pointer down event happens, a game object will be made active

#

But you could also set a public variable called pedalPressed to true, for example

#

You would then use that variable in Update to decide if the player should speed up

obtuse temple
#

Okay, sounds possible. Let me check how i can do it on code side. I understand what you write but i am not sure if i know exactly what i need to do

fast geyser
#
public class Car : MonoBehaviour {
  public bool accelerate;

  void Update() {
    if (accelerate) {
      // whatever makes the car go faster
    }
  }
}
#

e.g.

obtuse temple
#

Code looks now as your example. Is it normal the the Event Trigger script is filled out with the public variable name?

fast geyser
#

It looks like you dragged a script asset in there. That is incorrect.

#

You need to drag in a component from the scene

#

The script asset is just there to represent the actual script file

obtuse temple
#

Ahhhhh! Okay, hold on 🙂

fast geyser
#

Components in the inspector are the actual instances

obtuse temple
#

Feeling stupid af right now.
I dragged the component into the Event Trigger. How can i now link the "accelerate" varibale with this Event Trigger?

fast geyser
#

Open the dropdown (that currently says "No Function")

#

pick the name of the component, then pick the name of the variable

obtuse temple
#

The varible is not in the list

#

The component "Car" includes the carController script and the Car objects (tires and body)

fast geyser
#

Oh, right, I forgot. That's a list of methods.

#

most of those things are "properties" -- you use them like a variable, but they actually call a function

#

so when you do this.enabled = false;, it's actually calling a method

#

so all you need to do is add a method to set the accelerate flag

#
public void SetAccelerate(bool flag) {
  accelerate = flag;
}
#

That ought to do it.

obtuse temple
#

Should look like this then, correct?

fast geyser
#

Yep.

obtuse temple
#

One with true and one with false

#

Okay, no movement right now. I will check the code but that makes everything much clearer.
Thank you very much!

fast geyser
#

You're welcome!

#

Also, if you want to do something when you click the button (so, letting go after pressing on it), use the On Click event on the button itself

#

right now, it's just there as a visual

obtuse temple
#

I am currently using the On Click () for restarting the game. The Event Trigger Pointer Click would do the same, right?

fast geyser
#

I think so

#

Oh yeah, one other important thing

#

UI used to be part of Unity's core libraries (so, stuff in UnityEngine)

#

It got moved into its own package at some point

#

and then it won't exist in newer docs

obtuse temple
#

Okay, that's triggy. So i should always pay attention to the old docs if i can't find the stuff

fast geyser
#

nah, you should look for the docs on the new package doc website

#

The old docs will probably be mostly correct

#

but you'd rather see the new stuff

obtuse temple
#

Ahh, the Unity UI docs.
Okay, got it. Thank's for the hint!

Btw. the car is still stuck 😄
accelerate value changes on Pointer Down/Up but the car seems to have no fuel -.-

#
using System.Collections;

public class CarController : MonoBehaviour
{
    [SerializeField] private Rigidbody2D frontTire;
    [SerializeField] private Rigidbody2D backTire;
    [SerializeField] private float speed = 150f;
    public bool accelerate;

    private float moveInput;

    void Update()
    {
        if (accelerate) { 
            moveInput = Input.GetAxisRaw("Horizontal");
        }
    }
    void FixedUpdate()
    {
        frontTire.AddTorque(-moveInput * speed * Time.deltaTime);
        backTire.AddTorque(-moveInput * speed * Time.deltaTime);
    }

    public void SetAccelerate(bool flag)
    {
        Debug.Log(flag);
        accelerate = flag;
    }
}```
fast geyser
#

well, you're trying to use the Horizontal axis

#

but isn't the point that you're just pushing a button to make the car accelerate?

#

there's no horizontal input

obtuse temple
#

That makes sense. Puh. My brain..
Okay, will have to do some research then

fast geyser
#

also, don't use Time.deltaTime with AddTorque

#

Torque is how hard you're turning the wheel

#

If you're twisting the wheel with a newton-meter of torque, it doesn't matter how long each frame lasts

#

each frame, you tell the wheel you're currently twisting it that hard

#

it'll accelerate accordingly

fast geyser
obtuse temple
#

So just remove the "Time.deltaTime" because the "speed" variable is the constant amount?

fast geyser
#

It's like how AddForce works

#

you tell it how hard you're pushing

#

not how much the velocity is changing

#

the (angular) velocity change will depend on deltaTime

obtuse temple
obtuse temple
fast geyser
#

well, now you need something to make it slow down

obtuse temple
#

Ah nvm, you are correct. Only the one tire is moving (which is in the air). So the accleration stop works but it does not slow down the tires. Makes sense.
So many things to think of