Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Glitch when animating a gate opening

Discussion in 'Animation' started by ajlott, Mar 16, 2020.

  1. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    So I have this gate that I want to open when the player enters a trigger collider and close when the player exits. I have this working for the most part, but what happens is that the first time the player enters the trigger, the gate will pop down a bit and then raise to the correct open position and lower down to its correct starting position on exit. After that, the gate operates as expected with no popping down before opening. Any idea what might be causing this? Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RaiseGate : MonoBehaviour
    6. {
    7.     public GameObject gate;
    8.     public float raiseSpeed;
    9.  
    10.     private bool isGateRaised;
    11.     private float yPos;
    12.  
    13.     private void Start()
    14.     {
    15.         gate.transform.position = new Vector3(gate.transform.position.x, 2.5f, gate.transform.position.z);
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (isGateRaised && gate.transform.position.y < 9.4f)
    21.         {
    22.             yPos += raiseSpeed * Time.deltaTime;
    23.             gate.transform.position = new Vector3(gate.transform.position.x, yPos, gate.transform.position.z);
    24.         }
    25.  
    26.         if (!isGateRaised && gate.transform.position.y > 2.5f)
    27.         {
    28.             yPos -= raiseSpeed * Time.deltaTime;
    29.             gate.transform.position = new Vector3(gate.transform.position.x, yPos, gate.transform.position.z);
    30.         }
    31.     }
    32.  
    33.     private void OnTriggerEnter(Collider collider)
    34.     {
    35.         if (collider.gameObject.tag == "Player")
    36.         {
    37.             isGateRaised = true;
    38.             Debug.Log("Raise Gate");
    39.         }
    40.     }
    41.  
    42.     private void OnTriggerExit(Collider collider)
    43.     {
    44.         if (collider.gameObject.tag == "Player")
    45.         {
    46.             isGateRaised = false;
    47.             Debug.Log("Lower Gate");
    48.         }
    49.     }
    50. }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    You're not setting the yPost on Start, so when this starts happening:

    Code (csharp):
    1.  
    2.         if (isGateRaised && gate.transform.position.y < 9.4f)
    3.         {
    4.             yPos += raiseSpeed * Time.deltaTime;
    5.             gate.transform.position = new Vector3(gate.transform.position.x, yPos, gate.transform.position.z);
    6.         }
    7.  
    yPos starts out at 0f. You probably want to set it to 2.5f.

    I really recommend that you save the starting y-position of the gate, and then add to that, instead of hard-coding 2.5 and 9.4, though.
     
  3. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    The video isn't working...

    But i think your problem is that that variable ypos has no start value.
    So your gate starts the first time at 0;

    try changing :

    private float yPos;

    to

    private float yPos=2.5f;

    (line 11)

    hope it helps

    (edit) :) a fraction too late
     
  4. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    Thank you @Baste and @sylon for your replies. You are exactly correct. So obvious, too, can't believe I didn't see it.