#Event Trigger lets you run methods when
1 messages · Page 1 of 1 (latest)
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
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
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
public class Car : MonoBehaviour {
public bool accelerate;
void Update() {
if (accelerate) {
// whatever makes the car go faster
}
}
}
e.g.
Code looks now as your example. Is it normal the the Event Trigger script is filled out with the public variable name?
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
Ahhhhh! Okay, hold on 🙂
Components in the inspector are the actual instances
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?
Open the dropdown (that currently says "No Function")
pick the name of the component, then pick the name of the variable
The varible is not in the list
The component "Car" includes the carController script and the Car objects (tires and body)
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.
Should look like this then, correct?
Yep.
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!
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
I am currently using the On Click () for restarting the game. The Event Trigger Pointer Click would do the same, right?
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
So you'll find really old documentation like https://docs.unity3d.com/2018.2/Documentation/ScriptReference/EventSystems.EventTrigger.html
and then it won't exist in newer docs
So look for things here if you need to read about them: https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/index.html
Okay, that's triggy. So i should always pay attention to the old docs if i can't find the stuff
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
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;
}
}```
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
That makes sense. Puh. My brain..
Okay, will have to do some research then
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
You'll want to just use a constant amount of torque when the accelerate flag is true
So just remove the "Time.deltaTime" because the "speed" variable is the constant amount?
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
Ahhh, just make moveInput = 1 does the trick i guess 🙂
Ah, no. Car moves but never stopps
well, now you need something to make it slow down
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