mission-demolition/Assets/Scripts/Blocker.cs

44 lines
1.1 KiB
C#
Raw Normal View History

2024-02-22 02:08:26 -06:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blocker : MonoBehaviour
{
[Header("Set in Inspector")]
// Speed at which the AppleTree moves
public float speed = 1f;
// Distance where AppleTree turns around
public float leftAndRightEdge = 10f;
// Chance that the AppleTree will change directions
public float chanceToChangeDirections = 0.1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Basic Movement
Vector3 pos = transform.position;
pos.y += speed * Time.deltaTime;
transform.position = pos;
// Changing Direction
if (pos.y < -leftAndRightEdge) {
speed = Mathf.Abs(speed);
} else if (pos.y > leftAndRightEdge) {
speed = -Mathf.Abs(speed);
}
}
// Update exactly 50 times per second
void FixedUpdate()
{
if (Random.value < chanceToChangeDirections) {
speed *= -1;
}
}
}