aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Boulder.cs
blob: 5aa2beb816c81b741cc001ed863446bf7cea19e0 (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
41
42
43
44
45
using MontanaJohns.Actors;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boulder : MonoBehaviour
{
    [SerializeField] float speed;
    [SerializeField] float maxSpeed;

    private GameObject player;
    private Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        rb = transform.GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update()
    {
        if (player.transform.position.x < transform.position.x)
        {
            transform.Rotate(0, 0, 1);
        }
        else
        {
            transform.Rotate(0, 0, -1);
        }
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(player.transform.position.x, 0), speed * Time.deltaTime);
        if(rb.velocity.x >= maxSpeed)
            rb.velocity = new Vector2(maxSpeed, rb.velocity.y);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            collision.GetComponent<Actor>().TakeDamage(999);
        }
    }
}