Hello i want to do animation for doors but the void update is really fast. how i can slow down the void update
here is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class otevirani : MonoBehaviour
{
public Animator otevreni;
//Update is called once per frame
void Update()
{
otevreni.SetBool(“otevrit”, false);
Update occurs with the frame rate, so you can modify Application.targetFrameRate to slow it down. But almost certainly you are should redesign whatever you are doing if you think your problem can be resolved by reducing FPS.
I can’t tell what your problem is, because I don’t understand why you are setting the same variable to false over and over every frame. If what you want is a few frames to pass after setting it to true in OnCollisionEnter try using a timer, either in Update or a coroutine, instead of immediately setting it to false the next frame.
Please ensure you use the “Insert Code” button when adding code to your posts to make it easier to read.
I suggest you use Invoke for this problem, Invoke allows you to call a method after the specified amount of time.
I have written some code as an example for your scenario:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class otevirani : MonoBehaviour
{
public Animator otevreni;
public float doorOpeningDuration = 1f;
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Player"))
{
StartOpeningDoor();
Invoke("StopOpeningDoor", doorOpeningDuration);
}
}
void StartOpeningDoor()
{
otevreni.SetBool("otevrit", true);
}
void StopOpeningDoor()
{
otevreni.SetBool("otevrit", false);
}
}
This will play the door animation for 1 second by default because the Invoke calls StopOpeningDoor after 1 second, which sets the animator boolean to false.
Just a few notes:
Update is mainly for evaluating unpredictable behaviours such as Input from the player.
Coroutines should be used for kicking off a process which needs to do something every frame or a set of steps executed one after the other with timed intervals in between.