Search Unity

How can I animate both doors of the gate to be open to the left and right ?

Discussion in 'Animation' started by DubiDuboni, Feb 7, 2019.

  1. DubiDuboni


    Feb 5, 2019
    I want to make it in two ways.
    First using transform.localPosition and Lerp and second way by creating Animation clip.

    I have a parent GameObject empty name Gate and two children doorLeft and doorRight.
    On both doorLeft and doorRight there is a box collider and is trigger is checked.
    On doorLeft there is also a script name Collision:

    Code (csharp):
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Collision : MonoBehaviour
    6. {
    7.     public GameObject doorLeft;
    8.     public GameObject doorRight;
    9.     public GameObject player;
    10.     public bool doorLockState = false;
    11.     public float speed = 3;
    12.     private bool triggered = false;
    13.     private Animator playerAnimator;
    14.     private void Start()
    15.     {
    16.         playerAnimator = player.GetComponent<Animator>();
    17.     }
    18.     private void OnTriggerEnter(Collider other)
    19.     {
    20.         triggered = true;
    21.     }
    22.     private void OnTriggerExit(Collider other)
    23.     {
    24.         doorRight.transform.localPosition = Vector3.Lerp(doorRight.transform.localPosition, new Vector3(0, 0, 0), Time.deltaTime * speed);
    25.     }
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         float distanceFromTarget = Vector3.Distance(player.transform.position, doorLeft.transform.position);
    30.         if(triggered == true)
    31.         {
    32.             doorLeft.transform.localPosition = Vector3.Lerp(doorLeft.transform.localPosition, Vector3.up, Time.deltaTime * speed);
    33.         }
    34.     }
    35. }
    My FPSController ( Player ) have a box collder and is trigger unchecked. And also a Rigidbody.
    The idea is when the player trigger one of the doors open both doors. Either by using Lerp or by playing animation clip.

    Both ways don't work.
    The triggering is working. But when I'm using the line:

    Code (csharp):
    2. doorLeft.transform.localPosition = Vector3.Lerp(doorLeft.transform.localPosition, Vector3.up, Time.deltaTime * speed);
    No matter is I tried Vector3.up or left or forward it's always move to the back. The doorLeft never move to other direction and not to the left it seems like it's always moving to the back.

    And about the Animation clip on the Gate parent gameobject I created Animation clip.
    Added to the animation clip both doorLeft and doorRight positions properties.

    When I press/click the record button and change the doorLeft position it will always move from the far position to the right. Instead moving from it's default position to the left.

    I tried to play and move the keys either to the start or to the second 5 or tried to change the animation events moved them to the left and right on the chart. But I can't make it move from the original start position to the left. I recorded and pressed play to see the result and it's never moving from the original position to the left.

    And when recording I'm dragging the doorLeft from it's current original position to the left but when I play it it's start moving from the left to the right ( the original position ).

    My goal is to study how to animate the doors in both methods using Lerp or using Animation clip.
  2. DubiDuboni


    Feb 5, 2019
    Ok, I found how to make the Animation part. Working great using Animator and parameters and the gate open like I wanted.

    Now how to make the second method. If instead playing the Animation clip I want to use the Vector3.Lerp to make the gate open ?