Opening a door with OnCollisionStay
Hi, I'm trying to get a door to open by triggering a trigger parameter in the animator of the door when the character is colliding and the space bar is pressed. Nothing happens when I bring the character within the door's collider and press space. Maybe someone will have an idea for what I'm doing wrong. Thank you.`
using System.Collections; using System.Collections.Generic; using UnityEngine; public class DoorOpen : MonoBehaviour { Animator anim; int openHash = Animator.StringToHash("Open"); int closeHash = Animator.StringToHash("Close"); bool open = false; void Start() { anim = GetComponent<Animator>(); } void OnCollisionStay(Collision col) { if (Input.GetKeyDown("space")) { if (col.gameObject.name == "banker") { if (open == false) { anim.SetTrigger(openHash); open = true; } else { anim.SetTrigger(closeHash); open = false; } } } } }
I am going to need a little more information on the animators? Are you on 3D or 2D?
Answer by dgoelitz · Apr 25, 2018 at 08:35 AM
@Captain_Pineapple @Remy_Unity and @Karsten you seem to be answering questions right now. Can either of you help me?
@ZuhairGhias @Harinezumi @keckluis you look like you're answering questions now. Can either of you help me?
Answer by vinilly · Apr 25, 2018 at 09:21 AM
You need to first call the Collider of col.gameObject.name before declaring any inputs.
You can try:
void OnCollisionStay(Collision col)
{
if (col.gameObject.name == "banker" && Input.GetKeyDown("space"))
{
if (open == false)
{
anim.SetTrigger(openHash); open = true;
}
else
{
anim.SetTrigger(closeHash); open = false;
}
}
}
Unfortunately I can't try this right now but I will in the morning. Thank you so much.
You're welcome I'll try to be faster next time. Good nite friend :D
Doesn't work unfortunately. I'm testing it out with the animator open next to the game window and neither of the triggers ever get activated. Also I have two animations and whenever I select the door object with the second animation it switches back to the first one. Is it only possible to have one animation per object?
If it is 2D simply call from script.
public Sprite doorOpen, doorClose;
void OnCollisionStay(Collision col)
{
if (col.gameObject.name == "banker" && Input.Get$$anonymous$$eyDown("space"))
{
if (open == false)
{
GetComponent<SpriteRenderer>().sprite = doorOpen;
open = true;
StartCouroutine(ClosingDoor());
}
}
}
private IEnumerator ClosingDoor()
{
yield return new WaitForSeconds(5);
GetComponent<SpriteRenderer>().sprite = doorClose;
open = false;
}
This would Change the sprites better than an animator would for 2D.
So then I need a sprite of the door opening ins$$anonymous$$d of an animation?
Your answer
Follow this Question
Related Questions
How to make OnTriggerEnter Only work for certain Gameobjects? 2 Answers
How to enable specific event when other gameobject collides with the other prefab? 0 Answers
Navmesh agents not colliding with non trigger colliders? 0 Answers
Why won't this object destroy on collision? 1 Answer
why isnt my code working, 2 Answers