I have the same animation bool for multiple gameObjects with animations. They both play at the same time.
Here is my code for opening a door:
public class DoorOpenClose : MonoBehaviour {
public Animator doorControl;
public Camera camera;
private bool _isOpen = false;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if (Physics.Raycast(ray.origin, ray.direction, out raycastHit, Mathf.Infinity))
{
_isOpen = !_isOpen;
doorControl.SetBool("isOpening", _isOpen);
}
}
}
}
This script is attached to a button which then opens and closes a door successfully, the only problem is that another button with the same script, which is attached to a separate door opens when I click on a button.
tldr: Button 1 plays an animation, but Button 2 plays the same animation. Both buttons have the same scripts.
I can not find an answer (due to me being a beginner at code).
Answer by Cuttlas-U · Apr 27, 2020 at 07:58 AM
the problem is the "Update" function is being called on each script u have , so when u click the mouse both the scripts works and open their doors ,
for a quick and simple solution cause u are a beginner u can do like this :
delete the update code and use OnMouseDown() function ,
so write this instead :
void OnMouseDown()
{
isOpen = !_isOpen;
doorControl.SetBool("isOpening", _isOpen);
}
on mouse down is called once u click on a collider and only that collider :) Good Luck ;
Your answer
Follow this Question
Related Questions
How do I play an animation from another object C# 1 Answer
Trigger animation when looking at an object? 1 Answer
C# Door Script Problem 0 Answers