- Home /
Question by
nursedayuksel · Mar 06, 2021 at 04:01 PM ·
c#animationanimation controllerkeypress
Playing animations on different game objects with the same key press
Hi all,
In my game, I want the door and the fuse box to play their respective animations using the same key press (The key O in this case).
However, only the animation of the game object that I click FIRST plays. Even if I click on the other game object, the game object that I clicked first will play its animation.
I'm still a beginner in animation so I'm not sure when I'm doing wrong.
They have two different animation controllers.
This is the part of my Animation script that controls the raycast:
public Camera mainCamera;
private AnimationController raycastedObj;
private bool doDoorOnce;
private bool dofuseboxOnce;
void Update()
{
if (Input.GetKeyDown(KeyCode.O))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
{
if (hit.collider.CompareTag("Door"))
{
if (!doDoorOnce)
{
raycastedObj = hit.collider.gameObject.GetComponent<AnimationController>();
doDoorOnce = true;
}
raycastedObj.PlayDoorAnimation();
}
if (hit.collider.CompareTag("Fusebox"))
{
if (!dofuseboxOnce)
{
raycastedObj = hit.collider.gameObject.GetComponent<AnimationController>();
dofuseboxOnce = true;
}
raycastedObj.PlayFuseBoxCoverAnimation();
}
}
}
}
And this is my animation controller:
public void PlayDoorAnimation()
{
if (!doorOpen)
{
doorOpen = true;
Debug.Log("door opening");
doorAnim.SetTrigger("Open");
openingDoorAudio.Play();
}
else
{
doorOpen = false;
doorAnim.SetTrigger("Close");
closingDoorAudio.Play();
}
}
public void PlayFuseBoxCoverAnimation()
{
if (!fuseboxOpen)
{
Debug.Log("fusebox opening");
fuseboxOpen = true;
fuseboxCoverAnim.SetTrigger("Open");
}
else
{
fuseboxOpen = false;
fuseboxCoverAnim.SetTrigger("Close");
}
}
Where am I going wrong?
Comment