RotX18 / MP_Group4_EscapingReality

VR Escape room game developed in Unity
3 stars 0 forks source link

Key Object + Animations #76

Closed RotX18 closed 2 years ago

RotX18 commented 2 years ago

Credits

Animations and interaction of the objects were done with this issue by @RotX18

Objective:

RotX18 commented 2 years ago

Key and door interaction

Logic is as follows:

  1. Key spawns (#27)
  2. Key has an _unlocked boolean that will trigger the animation when the bool is set to true and the player releases the object
  3. When the key enters the DoorLock collider, the collider sets the unlocked boolean to true.
  4. When the key is released, if the key is near the door lock, it is parented to the door and the animation plays

Code + description

Key.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Key : MonoBehaviour, IPickable
{
    #region PUBLIC VARS
    public Animator anim;
    public GameObject door;
    #endregion

    #region PRIVATE VARS
    private bool _unlock = false;
    private int _doorUnlock = Animator.StringToHash("DoorUnlock");
    #endregion

    #region PROPERTIES
    #region IPickable PROPERTIES
    public IPickable.Controller CurrentController {
        get;
        set;
    }

    public bool Grabbed {
        get;
        set;
    } = false;
    #endregion

    public bool Unlock {
        get {
            return _unlock;
        }
        set {
            _unlock = value;
        }
    }
    #endregion

    #region INTERFACE METHODS
    public void OnRelease(){ 
        if(_unlock && !Grabbed){
            gameObject.transform.SetParent(door.transform);
            anim.SetTrigger(_doorUnlock);
        }
    }
    #endregion
}

Description: Key implements IPickable to facilitate unlocking animation

DoorLock.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorLock : MonoBehaviour
{
    private void OnTriggerEnter(Collider other) {
        if(other.name.Equals("Key")){
            other.GetComponent<Key>().Unlock = true;
        }
    }
}

Description:

Animation is bugged; Key does not animate when animation is triggered, to discuss with @RabbitKazma before updating issue and closing

RabbitKazma commented 2 years ago

Animation Key #80

image

RotX18 commented 2 years ago

Completed door unlocking animation

Implemented suggestion by @RabbitKazma to use a pre-parented key to run the animation

Code changes

Key.cs (modified code)

#region INTERFACE METHODS
public void OnRelease(){ 
    if(_unlock){
        Destroy(gameObject);
    }
}

private void OnDestroy() {
    Debug.Log("PLAYING DOOR ANIM");
    anim.SetTrigger(_doorUnlock);
}
#endregion

Edits:

NOTE: Will refactor, recommit and pull after this comment

Feature completed, issue will now be closed