aboutsummaryrefslogtreecommitdiff
blob: 874d6d360bae4bad04bfefb3667d21feaf62ef50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RockJaws : MonoBehaviour
{
    public Transform TopJaw;
    public Transform BottomJaw;
    public int Speed = 10;
    private bool reverse = false;
    private float initialTopJawY;
    // Start is called before the first frame update
    void Start()
    {
        initialTopJawY = TopJaw.localPosition.y;
    }

    // Update is called once per frame
    void Update()
    {

    }

    void FixedUpdate()
    {
        var topJawHeight= TopJaw.GetComponent<MeshRenderer>().bounds.size.y;
        var bottomJawHeight= BottomJaw.GetComponent<MeshRenderer>().bounds.size.y;

        if (TopJaw.localPosition.y - topJawHeight / 2 < BottomJaw.localPosition.y + bottomJawHeight / 2) reverse = true;
        else if(TopJaw.localPosition.y > initialTopJawY) reverse = false;   

        if (!reverse) {
            TopJaw.Translate(0, -0.01f * Speed, 0, Space.World);
            BottomJaw.Translate(0, 0.01f * Speed, 0, Space.World);
        } else {
            TopJaw.Translate(0, 0.01f * Speed, 0, Space.World);
            BottomJaw.Translate(0, -0.01f * Speed, 0, Space.World);
        }
    }
}