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

public class Ruch : MonoBehaviour
{
    [SerializeField] private GameObject kapsula, kula;
    //warto wspczynnika predkoci
    //i momentu siy
    [SerializeField] private float v = 2.0f,
                                  moment=500.0f;
    private GameObject aktywny;

    void toczenie(Rigidbody rb,Vector3 momentObrotowy)
    {
        //dodaj moment obrotowy
        rb.AddTorque(momentObrotowy);
    }
    void aktywnyObiekt()
    {
        //wybrano klawisz 1
        if (Input.GetKeyUp(KeyCode.Alpha1))
        {
            aktywny = kapsula;
            return;
        }
        //wybrano klawisz 2
        if (Input.GetKeyUp(KeyCode.Alpha2))
        {
            aktywny = kula;
            return;
        }
    }

    void ruszaj()
    {
        float kierunek;
        //reaguje na klawisze A,D i strzaki w Lewo, Prawo
        if ((kierunek = Input.GetAxis("Horizontal")) != 0)
        {
            if(aktywny!=kula)
            aktywny.transform.Translate(-kierunek*Time.deltaTime*v,0,0);
            else
            {
                Vector3 momentobrotowy=new Vector3(0, 0, kierunek * Time.deltaTime * moment);
                aktywny.GetComponent<Rigidbody>().AddTorque(momentobrotowy);
            }
        }
        if ((kierunek = Input.GetAxis("Vertical")) != 0)
        {
            if (aktywny != kula)
                aktywny.transform.Translate(0,0,-kierunek * Time.deltaTime * v);
            else
            {
                Vector3 momentobrotowy = new Vector3(kierunek * Time.deltaTime * moment,0,0);
                aktywny.GetComponent<Rigidbody>().AddTorque(momentobrotowy);
            }
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        aktywny = kapsula;
    }

    // Update is called once per frame
    void Update()
    {
        aktywnyObiekt();
        ruszaj();
    }
}
