﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Tap.Tilt
{
    [RequireComponent(typeof(Rigidbody))]
    public class ZigZagPhysics : MonoBehaviour
    {
        public float maxDeviation = 0.1f;

        public Vector3 forceDirection;

        private Rigidbody rigid;

        // Start is called before the first frame update
        void Awake()
        {
            rigid = GetComponent<Rigidbody>();
        }
        // Update is called once per frame
        void FixedUpdate()
        {
            rigid.velocity =
                new Vector3(
                    forceDirection.x != 0 ?
                        Random.Range(rigid.velocity.x - maxDeviation, rigid.velocity.x + maxDeviation)
                        : rigid.velocity.x,
                    forceDirection.y != 0 ?
                        Random.Range(rigid.velocity.y - maxDeviation, rigid.velocity.y + maxDeviation)
                        : rigid.velocity.y,
                    forceDirection.z != 0 ?
                        Random.Range(rigid.velocity.z - maxDeviation, rigid.velocity.z + maxDeviation)
                        : rigid.velocity.z);
        }
    }
}